2016-11-13 2 views
-2

Im neu zu C# und LinQ und im mit einem "System.InvalidCastException" bekommen, wenn im ein Wörterbuch aus meinem LinQKonvertieren von Linq ToDictionary C#

Das ist mein Objekt

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; } 
} 

Dies ist zu machen versuchen, mein Code:

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; 
      }); 

Mein Wunsch Ausgabe lautet: Dictionary<Anonymous(string,int,double),Dictionary<int,int>> Was mache ich falsch?

Vielen Dank im Voraus.

+0

was ist die gewünschte Ausgabe? –

+0

Ich möchte das Wörterbuch in einer Foreach-Anweisung in einer Liste speichern –

+0

Was ist der gewünschte Ausgabetyp? 'Wörterbuch >'? –

Antwort

0

Es gibt einen Unterschied zwischen Ihrer Frage und Code gepostet, überprüfen Sie bitte die Arbeitsversion von Code, der am Ende der folgenden Struktur führt:

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

Warum die obige Struktur, nicht die, die Sie haben im Code geschrieben: Dictionary<Anonymous(string,int,double),Dictionary<int,int>>

Da Sie 7 fields im Schlüssel haben, und sie werden Teil anonymous type im Schlüssel werden, die Kombination dieser sieben Felder bedeutet, ist Wert ALW ays einzigartig und kann zum Gruppieren der Daten

Arbeits Code mit Daten (unter Verwendung von LINQPad, die eine Dump-Methode liefert drucken) als Schlüssel verwendet werden

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; 
    } 
} 

Leitfrage

Code, den ich geschrieben habe, ist die Validierung der oben genannten Code richtig funktioniert, in meinem Verständnis Sie weiter suchen aggregieren den Tag weise und multiplizieren mit Price und erhalten einen Gesamtpreis Wert für eine bestimmte Tastenkombination, und speichern Sie es in einem List für die Sie ein folgendes Modell benötigen:

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; } 
} 
  • folgenden Code in Fortsetzung des obigen Codes wird es füllen, die meisten Komponenten sind Teil des Key, Bedürfnisse Berechnung verbleibende und ich gehe davon aus Precio ist die PriceTotalPrice für die Berechnung:

    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();