在LINQ Lambda表达式中使用GroupBy,Count和Sum

我有一系列属性重量,体积和所有者的箱子。

我想使用LINQ来获取盒子信息的概要列表(由所有者)

例如

**Owner, Boxes, Total Weight, Total Volume**  
Jim,     5,     1430.00,      3.65  
George,  2,     37.50,        1.22

有人可以告诉我如何用Lambda表达式做到这一点吗?


    var ListByOwner = list.GroupBy(l => l.Owner)
                          .Select(lg => 
                                new { 
                                    Owner = lg.Key, 
                                    Boxes = lg.Count(),
                                    TotalWeight = lg.Sum(w => w.Weight), 
                                    TotalVolume = lg.Sum(w => w.Volume) 
                                });

        var q = from b in listOfBoxes
                group b by b.Owner into g
                select new
                           {
                               Owner = g.Key,
                               Boxes = g.Count(),
                               TotalWeight = g.Sum(item => item.Weight),
                               TotalVolume = g.Sum(item => item.Volume)
                           };

var boxSummary = from b in boxes
                 group b by b.Owner into g
                 let nrBoxes = g.Count()
                 let totalWeight = g.Sum(w => w.Weight)
                 let totalVolume = g.Sum(v => v.Volume)
                 select new { Owner = g.Key, Boxes = nrBoxes,
                              TotalWeight = totalWeight,
                              TotalVolume = totalVolume }
链接地址: http://www.djcxy.com/p/34285.html

上一篇: Using GroupBy, Count and Sum in LINQ Lambda Expressions

下一篇: Parsing and Translating Java 8 lambda expressions