LINQ help
-
Tuesday, April 12, 2011 2:29 AM
Let say I have a Product entity and it has a Price property.
I have a list of product List<Product> products. How can I use LINQ to group a list of Products that sum up to no more than $100 and then return a Dictionary object of each group
For example:
product.price = 30
product1.price = 60
product2.price = 10
product3.price = 50
product4.price = 20
product5.price = 60
product6.pricr = 40So with that list I want to return a dictionary object as follow:
Dictionary<1, List{product,product1}>
Dictionary<2, List{product2,product3,product4}>
Dictionary<3, List{product5,product6}>Any ideas??
All Replies
-
Wednesday, April 13, 2011 6:11 PM
Elaborate Your Query.
Do You Want To Sum Up Products According To Specific Criteria? And Only Return Those Which Are Less Then 100 ???
Products .GroupBy(x => x.ProductType) .Select(grp => new { SumPrize = grp.Sum(x => x.Prize), ProductType = grp.First().ProductType }) .Where(x => x.SumPrize <= 100) .ToDictionary(x => x.ProductType, x => x.SumPrize);
Mark Post As Answer If It Helped You and Also Take Some Time Out To Mark The Thread Resolved.

