0

I have a list of values and within this there is another list that I place a series of values with their date in UnixTime.

List code.

public class ValorES
{
    public int Fecha { get; set; }
    public decimal valor { get; set; }
}
public class DatosEntSal
{
    public string PuntoMed { get; set; }
    public int NumId { get; set; }
    public string Nombre { get; set; }
    public string Etiqueta { get; set; }
    public string Unidad { get; set; }
    public string UmbralInferior { get; set; }
    public string BandaMuertaInferior { get; set; }
    public string UmbralSuperior { get; set; }
    public string BandaMuertaSuperior { get; set; }
    public string EscalaEjeMin { get; set; }
    public string EscalaEjeMax { get; set; }
    public bool AutoEscalaEje { get; set; }
    public int Pos_X { get; set; }
    public int Pos_Y { get; set; }
    public int Alto { get; set; }
    public int Ancho { get; set; }
    public string MimicoId { get; set; }
    public List<ValorES> ValoresRegistrados { get; set; }
}

The code is the following:

public List<DatosEntSal> ObtenerEntradasSalidas()
{
    int _contrato = 14;
    List<DatosEntSal> _ValoresEntSal = new List<DatosEntSal>();
    List<EntradaSalida> EntradasSalidas = new List<EntradaSalida>();
    List<Vista> EntSalVista = new List<Vista>();

    List<Registro> data = new List<Registro>();

    MonitoreoEntities _context = new MonitoreoEntities();
    EntSalVista = _context.Vistas.Where(yy => yy.ID_EntSal != null).ToList();

    foreach (var Valor_EntSal in EntSalVista)
    {
        string fecha;
        fecha = Fecha_Ultimo_Registros(EntSalID_PtoMedId(Valor_EntSal.ID_EntSal), Valor_EntSal.ID_EntSal);
        DateTime _FechaFinal = DateTime.Parse(fecha);
        DateTime _FechaInicial = _FechaFinal;
        List<ValorES> datosReg = new List<ValorES>();
        int _PtoMedId=EntSalID_PtoMedId(Valor_EntSal.ID_EntSal);
        string _NombrePtoMed=PuntoMedID_NombrePtoMed(_PtoMedId);                   
        EntradasSalidas = _context.EntradaSalidas.Where(xx => xx.PuntoDeMedicionId == _PtoMedId
            && xx.Graficar == 1 && xx.Id == Valor_EntSal.ID_EntSal).ToList();                    
        if (EntradasSalidas.Count > 0)
        {
            data = _context.Registroes.Where(a => a.EntradaSalidaId == Valor_EntSal.ID_EntSal
            && a.FechaHoraRegistro <= _FechaFinal
            && a.FechaHoraRegistro >= _FechaInicial).OrderBy(i => i.FechaHoraRegistro).ToList();

            foreach (var item in data)
            {
                datosReg.Add(new ValorES()
                {
                    Fecha = item.FechaHoraCreacion.Value,
                    valor = item.Valor
                });
            };

            _ValoresEntSal.Add(new DatosEntSal()
            {
                PuntoMed = _NombrePtoMed,
                NumId =EntradasSalidas[0].Id,// _ESVista.Id,
                Nombre =EntradasSalidas[0].Nombre,// _ESVista.Nombre,
                Etiqueta =EntradasSalidas[0].Etiqueta,// _ESVista.Etiqueta,
                Unidad =EntradasSalidas[0].Unidad,// _ESVista.Unidad,
                UmbralInferior =EntradasSalidas[0].UmbralInferior.ToString(),// _ESVista.UmbralInferior.ToString(),
                BandaMuertaInferior = EntradasSalidas[0].BandaMuertaInferior.ToString(),
                UmbralSuperior = EntradasSalidas[0].UmbralSuperior.ToString(),
                BandaMuertaSuperior = EntradasSalidas[0].BandaMuertaSuperior.ToString(),
                EscalaEjeMin = EntradasSalidas[0].EscalaEjeMin.ToString(),
                EscalaEjeMax = EntradasSalidas[0].EscalaEjeMax.ToString(),
                AutoEscalaEje = EntradasSalidas[0].AutoEscalaEje.Value,
                Pos_X = Valor_EntSal.Pos_X,
                Pos_Y = Valor_EntSal.Pos_Y,
                Alto = Valor_EntSal.Alto,
                Ancho = Valor_EntSal.Ancho,
                MimicoId = Valor_EntSal.MimicoCodigo,
                ValoresRegistrados = datosReg,
            });
        };

        datosReg.Clear(); //limpio la lista
    }
    return _ValoresEntSal;
}

Problem: When I run the program step by step in the loop I get all the values from the "ValueES" list.

Then I insert these values in the list of "DatosEntSal", at the end of the If, I see and the internal list "RegisteredValues", this has all the elements of "ValorES", but when leaving the loop and continue with the foreach to take another element , it deletes all the elements of the internal list "Registered Values" and leaves only one.

I run the program step by step, after 5 interactions, I have:

  • _ValoresEntSal Count = 5 System.Collections.Generic.List
  • [0] {SystemaControl.WebServices.ValoresVista1.DatosEntSal} SystemaControl.WebServices.ValoresVista1.DatosEntSal
  • [1] {SystemaControl.WebServices.ValoresVista1.DatosEntSal} SystemaControl.WebServices.ValoresVista1.DatosEntSal
  • [2] {SystemaControl.WebServices.ValoresVista1.DatosEntSal} SystemaControl.WebServices.ValoresVista1.DatosEntSal
  • [3] {SystemaControl.WebServices.ValoresVista1.DatosEntSal} SystemaControl.WebServices.ValoresVista1.DatosEntSal
  • [4] {SystemaControl.WebServices.ValoresVista1.DatosEntSal} SystemaControl.WebServices.ValoresVista1.DatosEntSal

In iteration 5, the values are:

[4] {SystemaControl.WebServices.ValoresVista1.DatosEntSal}  SystemaControl.WebServices.ValoresVista1.DatosEntSal
Alto    3   int
Ancho   6   int
AutoEscalaEje   false   bool
BandaMuertaInferior "11.00" string
BandaMuertaSuperior "22.00" string
EscalaEjeMax    "35.00" string
EscalaEjeMin    "0.00"  string
Etiqueta    "A3"    string
MimicoId    "gl01"  string
Nombre  "Cava A2 Temp"  string
NumId   277 int
Pos_X   0   int
Pos_Y   4   int
PuntoMed    "CavasLujan"    string
UmbralInferior  "12.00" string
UmbralSuperior  "23.00" string
Unidad  "°C"    string
ValoresRegistrados  Count = 97  System.Collections.Generic.List<SystemaControl.WebServices.ValoresVista1.ValorES>

It is seen that the object "ValoresRegistrados" has 97 elements, but when leaving the if loop, it remains with 0 elements.

[4] {SystemaControl.WebServices.ValoresVista1.DatosEntSal}  SystemaControl.WebServices.ValoresVista1.DatosEntSal
Alto    3   int
Ancho   6   int
AutoEscalaEje   false   bool
BandaMuertaInferior "11.00" string
BandaMuertaSuperior "22.00" string
EscalaEjeMax    "35.00" string
EscalaEjeMin    "0.00"  string
Etiqueta    "A3"    string
MimicoId    "gl01"  string
Nombre  "Cava A2 Temp"  string
NumId   277 int
Pos_X   0   int
Pos_Y   4   int
PuntoMed    "CavasLujan"    string
UmbralInferior  "12.00" string
UmbralSuperior  "23.00" string
Unidad  "°C"    string
ValoresRegistrados  Count = 0   System.Collections.Generic.List<SystemaControl.WebServices.ValoresVista1.ValorES>
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
PabloB
  • 19
  • 7
  • I would advise to reduce code/data in this question to target the exact problem at a smaller scale. For example, leaving like two columns should be sufficient. – Andrius Naruševičius Jul 30 '18 at 21:12
  • Possible duplicate of [How do I change my new list without changing the original list?](https://stackoverflow.com/questions/13977841/how-do-i-change-my-new-list-without-changing-the-original-list) – mjwills Jul 30 '18 at 21:41
  • One alternative solution is adding not the list you want to clear but a copy of it: `ValoresRegistrados = datosReg.ToList();` – TaW Jul 31 '18 at 04:20

2 Answers2

1

At the end of the foreach iteration, you have:

datosReg.Clear();

That clears the list you just assigned to the new DatosEntSal instance. You don't need the call to Clear() as the variable is declared inside the foreach, just remove it.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
1

You are referring to the list as a reference and thus clearing variable datosReg also clears ValoresRegistrados. Create a new list out of it if you want it to maintain its values like so:

ValoresRegistrados = datosReg.ToList()
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78