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

在LINQ Lambda表达式中使用GroupBy、Count和Sum,linq,count,lambda,group-by,sum,Linq,Count,Lambda,Group By,Sum,我收集了一些箱子,包括重量、体积和主人 我想使用LINQ获得盒子信息的汇总列表(按所有者) e、 g 有人能告诉我如何使用Lambda表达式吗 var q = from b in listOfBoxes group b by b.Owner into g select new { Owner = g.K

我收集了一些箱子,包括重量、体积和主人

我想使用LINQ获得盒子信息的汇总列表(按所有者)

e、 g

有人能告诉我如何使用Lambda表达式吗

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