Converting Linq toDictionary C# -


im new c# , linq , im getting "system.invalidcastexception" when im trying make dictionary out of linq

this object

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

this 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>();                   (int = 1; <= 30; i++)                      dishdictionary[i] = 0;                   foreach (var grp in dishgrp)                  {                      dishdictionary[grp.fecha.day] =                      grp.cantidad;                  }                   return dishdictionary;              }); 

my desire output : dictionary<anonymous(string,int,double),dictionary<int,int>> doing wrong?

thanks in advance.

there's difference between question , code posted, please check working version of code leads following structure in end:

dictionary<anonymous(string,string,string,string,int,string,double),dictionary<int,int>> 

why above structure, not 1 have posted in code : dictionary<anonymous(string,int,double),dictionary<int,int>>

since having 7 fields in key , become part of anonymous type in key, means combination of these 7 fields value unique , can used key grouping data

working code data (using linqpad, provides dump method print)

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>();                       (int = 1; <= 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;     } } 

main question

code have posted validation of above code working correctly, in understanding further looking aggregate day wise quantity , multiply price , total price value given key combination, , save in list need following model:

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; } } 
  • following code in continuation of above code fill up, of components part of key, remaining needs calculation , assume precio price calculating 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(); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -