How to make a SUM of Dictionary Value nested into a list with LINQ ?

Posted by user551108 on Stack Overflow See other posts from Stack Overflow or by user551108
Published on 2010-12-22T11:44:25Z Indexed on 2010/12/22 11:54 UTC
Read the original article Hit count: 107

Filed under:
|

Hi All, I have a product object declared as :

Product {
   int ProductID;
   string ProductName;
   int ProductTypeID;
   string ProductTypeName;
   int UnitsSold
   Dictionary <string, int> UnitsSoldByYear;
}

I want to make a sum on UnitsSold and UnitsSoldByYear properties with a Linq query but I didn't know how to make this kind of sum on a dictionary ! Here is my begining linq query code :

var ProductTypeSum = from i in ProductsList
                     group i by new { i.ProductTypeID, i.ProductTypeName} into pt
                     select new
                         {
                             ProductTypeID= pt.Key.ProductTypeID,
                             ProductTypeName= pt.Key.ProductTypeName,
                             UnitsSoldSum= pt.Sum(i => i.UnitsSold),
                             // How to make a Dictionary sum here
                            }

Thank you for your help !

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ