2016-11-13 51 views
-2

进出口新的C#和LINQ和即时得到一个“System.InvalidCastException”当我尝试做一个解释了我的LINQ转换的LINQ toDictionary C#

这是我的对象

public class Pdv 
{ 
    public DateTime Fecha { get; set; } 

    public string Clave_PDV { get; set; } 

    public string Nombre_Pdv { get; set; } 

    public string Turno { get; set; } 

    public string Nombre_Turno { get; set; } 

    public int Platillo { get; set; } 

    public string Nombre_Platillo { get; set; } 

    public int Cantidad { get; set; } 

    public double Precio { get; set; } 

    public double Total { get; set; } 
} 

这是我的代码:

var dishesGrouping = 
     db_pdv.Pdv.GroupBy(d => new { d.Clave_PDV, d.Nombre_Pdv, d.Turno, d.Nombre_Turno, d.Platillo, d.Nombre_Platillo, d.Precio }); 

var dishGroupingDictionary = dishesGrouping 
      .ToDictionary(dishGrp => dishGrp.Key, dishGrp => 
      { 
       var dishDictionary = new Dictionary<int, int>(); 

       for (int i = 1; i <= 30; i++) 
        dishDictionary[i] = 0; 

       foreach (var grp in dishGrp) 
       { 
        dishDictionary[grp.Fecha.Day] = 
        grp.Cantidad; 
       } 

       return dishDictionary; 
      }); 

我的愿望输出为:Dictionary<Anonymous(string,int,double),Dictionary<int,int>> 我在做什么错?

在此先感谢。

+0

什么是所需的输出? –

+0

我想将字典保存到foreach语句中的列表 –

+0

期望的输出类型是什么? '词典>'? –

回答

0

有你的问题和代码发布之间的差异,请检查导致以下结构到底代码的工作版本:

Dictionary<Anonymous(string,string,string,string,int,string,double),Dictionary<int,int>> 

为什么上面的结构,而不是你有一个张贴在代码:Dictionary<Anonymous(string,int,double),Dictionary<int,int>>

由于要具有在键7 fields和他们将在关键的,这意味着这些7个字段的组合成为anonymous type部分值是ALW AYS独特并且可以用作键用于分组数据

工作用数据(使用Linqpad,它提供一个转储的方法来打印)码

void Main() 
{ 
    List<Pdv> pdvList = Pdv.FetchList(); 

    var dishesGrouping = 
     pdvList.GroupBy(d => new { d.Clave_PDV, d.Nombre_Pdv, d.Turno, d.Nombre_Turno, d.Platillo, d.Nombre_Platillo, d.Precio }); 

    var dishGroupingDictionary = dishesGrouping 
       .ToDictionary(dishGrp => dishGrp.Key, dishGrp => 
       { 
        var dishDictionary = new Dictionary<int, int>(); 

        for (int i = 1; i <= 30; i++) 
         dishDictionary[i] = 0; 

        foreach (var grp in dishGrp) 
        { 
         dishDictionary[grp.Fecha.Day] = 
         grp.Cantidad; 
        } 

        return dishDictionary; 
       }); 

    dishGroupingDictionary.Dump(); 
} 

public class Pdv 
{ 
    public DateTime Fecha { get; set; } 

    public string Clave_PDV { get; set; } 

    public string Nombre_Pdv { get; set; } 

    public string Turno { get; set; } 

    public string Nombre_Turno { get; set; } 

    public int Platillo { get; set; } 

    public string Nombre_Platillo { get; set; } 

    public int Cantidad { get; set; } 

    public double Precio { get; set; } 

    public double Total { get; set; } 

    public static List<Pdv> FetchList() 
    { 
     List<Pdv> pdvList = new List<Pdv>(); 

     pdvList.Add(new Pdv { Fecha = new DateTime(2016, 11, 1), Clave_PDV = "M", Nombre_Pdv = "M", Turno = "M",Nombre_Turno = "M",Platillo = 5,Nombre_Platillo = "M",Cantidad = 2,Precio = 5.0,Total = 20.0}); 
     pdvList.Add(new Pdv { Fecha = new DateTime(2016, 11, 2), Clave_PDV = "N", Nombre_Pdv = "N", Turno = "N",Nombre_Turno = "M",Platillo = 5,Nombre_Platillo = "M",Cantidad = 2,Precio = 5.0,Total = 20.0}); 
     pdvList.Add(new Pdv { Fecha = new DateTime(2016, 11, 3), Clave_PDV = "O", Nombre_Pdv = "O", Turno = "O",Nombre_Turno = "M",Platillo = 5,Nombre_Platillo = "M",Cantidad = 2,Precio = 5.0,Total = 20.0}); 
     pdvList.Add(new Pdv { Fecha = new DateTime(2016, 11, 4), Clave_PDV = "P", Nombre_Pdv = "P", Turno = "P",Nombre_Turno = "M",Platillo = 5,Nombre_Platillo = "M",Cantidad = 2,Precio = 5.0,Total = 20.0}); 
     pdvList.Add(new Pdv { Fecha = new DateTime(2016, 11, 5), Clave_PDV = "Q", Nombre_Pdv = "Q", Turno = "Q",Nombre_Turno = "M",Platillo = 5,Nombre_Platillo = "M",Cantidad = 2,Precio = 5.0,Total = 20.0}); 
     pdvList.Add(new Pdv { Fecha = new DateTime(2016, 11, 6), Clave_PDV = "R", Nombre_Pdv = "R", Turno = "R",Nombre_Turno = "M",Platillo = 5,Nombre_Platillo = "M",Cantidad = 2,Precio = 5.0,Total = 20.0}); 
     pdvList.Add(new Pdv { Fecha = new DateTime(2016, 11, 7), Clave_PDV = "S", Nombre_Pdv = "S", Turno = "S",Nombre_Turno = "M",Platillo = 5,Nombre_Platillo = "M",Cantidad = 2,Precio = 5.0,Total = 20.0}); 

     return pdvList; 
    } 
} 

主要问题

我发布的代码是上述代码正确工作的验证,根据我的理解,您正在进一步考虑整天明智的Quantity并与Price相乘,并获得给定组合键的总价格值,并且它保存在您需要以下模型List

public class Pdv_Result 
{ 
    public string Clave_PDV { get; set; } 

    public string Nombre_Pdv { get; set; } 

    public string Turno { get; set; } 

    public string Nombre_Turno { get; set; } 

    public int Platillo { get; set; } 

    public string Nombre_Platillo { get; set; } 

    public double Precio { get; set; } 

    public int TotalQuantity { get; set; } 

    public double TotalPrice { get; set; } 
} 
  • 按照以上代码的延续代码将它填平,大部分组件是Key的一部分,剩余的需求计算和我认为PrecioPrice计算TotalPrice

    var pdvResultList =  
         dishGroupingDictionary.Select(kv => new Pdv_Result 
                { 
                 Clave_PDV = kv.Key.Clave_PDV, 
                 Nombre_Pdv = kv.Key.Nombre_Pdv 
                 Turno = kv.Key.Turno, 
                 Nombre_Turno = kv.Key.Nombre_Turno, 
                 Platillo = kv.Key.Platillo, 
                 Nombre_Platillo = kv.Key.Nombre_Platillo, 
                 Precio = kv.Key.Precio, 
                 TotalQuantity = kv.Value.Sum(kvChild => kvChild.Value), 
                 TotalPrice = kv.Key.Precio * kv.Value.Sum(kvChild => kvChild.Value) 
                } 
                ).ToList();