Arturo Martinez Arturo Martinez - 22 days ago 5
C# Question

Empty data on List in LinQ Statement

I got this LinQ statement

var daysList = new List<int>(new int[30]);

var model_pdv = db_pdv.Pdv.GroupBy(x => new { Pdv = x.Clave_PDV, Nombre_Pdv = x.Nombre_Pdv})
.Select(x => new DishVM()
{
Clave_PDV = x.Key.Pdv,
Nombre_Pdv = x.Key.Nombre_Pdv,
Days = daysList,
Data = x
}).ToList();


However i dont know why my "Data" List inside my LinQ gets empty values the first time but then i saves the LinQ as it should

This is my DishVm Class:

public class DishVM
{
public string Clave_PDV { get; set; }
public string Nombre_Pdv { get; set; }
public IEnumerable<Pdv> Data { get; set; }
}


And my Pdv class:

public class Pdv
{
public string Clave_PDV { get; set; }
public string Nombre_Pdv { get; set; }
}


How can i avoid the empty Data List?

Answer

The type of x within your Select statement is IGrouping, change it to produce a list:

var model_pdv = db_pdv.Pdv.GroupBy(x => new { Pdv = x.Clave_PDV, Nombre_Pdv = x.Nombre_Pdv})
            .Select(x => new DishVM()
            {
                Clave_PDV = x.Key.Pdv,
                Nombre_Pdv = x.Key.Nombre_Pdv,
                Days = daysList,
                Data = x.ToList()
            }).ToList();
Comments