2016-11-13 48 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();