LINQ的使用

汇总列表中各物品的数量

var list = (from t in lst
group t by t.SparePartNo into g
select new { g.Key, sum = g.Sum(p => p.SparePartQty), Total = g.Count() }).ToList();

 

查找每个类别中单价高于该类别平均单价的产品:

 1 var categories =
 2     from p in db.Products
 3     group p by p.CategoryID into g
 4     select new {
 5         g.Key, 
 6         ExpensiveProducts =
 7             from p2 in g
 8             where p2.UnitPrice > g.Average(p3 => p3.UnitPrice)
 9             select p2
10     };
View Code

 

posted @ 2020-07-08 15:21  德平Zeng  阅读(92)  评论(0编辑  收藏  举报