Variable interfering with value of other variable

Asked

Viewed 63 times

0

Good afternoon Gentlemen.

I’m facing a problem I’ve never seen before, below I have a view mapped in the EF with effluent

var viewRelatorio = db.view.where(ra.DataBruta.Month == mesAno.Month).toList();

I have two dictionaries that will interact with the data in this view

var rel1 = new Dictionary<int, Relatorio>();
var relItemVirada = new Dictionary<int, Relatorio>();

var relatorio = new Relatorio>();
var eventoFinal = false

I read the view for iteration with dictionaries

foreach (var item in viewRelatorio)
{
  rel1.Add(i, item);                                       
  if (item.HorarioInicial.StartsWith("23") &&  item.HorarioFinal.StartsWith("00"))
  {
    relatorio = item;
    relatorio.HorarioFinal = "235959";
    relItemVirada.Add(i, relatorio);        
  }
  i++;
}

What happens is that every time I change the value in report.Timefinal it changes the original value in item that does not undergo any kind of change, I would like to know what this might be ?

I have tried to interact in several ways, different loops, directly in the item, whenever I touch an item affects the next or vice and versa

follows the Fluent mapping

public RelatorioConfiguration()
        : base("name=" + ConfigurationManager.AppSettings["databaseInstance"].ToString())
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var relatorio = modelBuilder.Entity<Relatorio>();

        relatorio.ToTable("view_Relatorio");

        relatorio.HasKey(p => new {p.DataBruta, p.HorarioInicial});
        relatorio.Property(p => p.Ano);
        relatorio.Property(p => p.Classificacao);
        relatorio.Property(p => p.DataBruta);
        relatorio.Property(p => p.HorarioFinal);
        relatorio.Property(p => p.HorarioInicial);
    }

    public DbSet<Relatorio> Relatorio { get; set; }
  • I don’t know if it has anything to do with it, but it could be the Entity Changetracker understanding that you are editing a table item, try using db.view.Asnotracking(). Where(ra.DataBruta.Month == mesAno.Month). toList();' thus disables Changetracker

1 answer

3


If I understand correctly, your problem is not in EF but rather because classes are by reference and not by value in . Net.

I will heed your question:

What happens is that every time I change the value in report.Horariofinal he changes the original value in item who is not undergoing any kind of change, I would like to know what that might be ?

Seeing your code relatorio = item; When you assign "item" to another "report" variable you do not "copy" the item but only a reference and so the two variables at this time point to the same object. Soon if you change a "report" property, you will change it to "item" as well. Hence this code relatorio.HorarioFinal = "235959" is also changing the value of "item.Timefinal".

For example I did an example on Dotnetfiddle https://dotnetfiddle.net/wjaaAm.

One solution is to perform a clone of the object and one of the ways to do this is by using Binaryformatter, follow an example of an Extension code below:

    public static T DeepClone<T>(this T source) where T : class
    {
        using (Stream cloneStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();

            formatter.Serialize(cloneStream, source);
            cloneStream.Position = 0;
            T clone = (T)formatter.Deserialize(cloneStream);

            return clone;
        }
    }
}
  • I imagined it to be something similar to this, but how would the copy of the item effectively and not the pointing to the reference ?

  • @Leo edited my answer to contain a possible solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.