1、单字段分组并求和:
var list = data.GroupBy(g => g.GoodsId).Select(e => new { GoodsId = e.Key, Qty = e.Sum(q => q.Qty) });
2、多字段分组求和:
var list = data.GroupBy(g => new { g.StorageId,g.GoodsId }).Select(e => new { GoodsId = e.Key, Qty = e.Sum(q => q.Qty) });
求平均值直接把Sum更换成:Average即可。
3、具有导航属性的类求和及分组:
var list = data.GroupBy(g => new { g.StorageId,g.GoodsId }).Select(e => new { StorageName = e.Select(i => i.Storage.Name).FirstOrDefault(), GoodsName = e.Select(i => i.Goods.Name).FirstOrDefault(), CategoryName = e.Select(i => i.Goods.CategoryName).FirstOrDefault(), Qty = e.Average(q => q.Qty) });