Using GroupBy, Count and Sum in LINQ Lambda Expressions

前端 未结 3 716
星月不相逢
星月不相逢 2020-12-04 13:34

I have a collection of boxes with the properties weight, volume and owner.

I want to use LINQ to get a summarized list (by owner) of the box information

e.g.

3条回答
  •  离开以前
    2020-12-04 14:03

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

提交回复
热议问题