C# linq group by
程序员文章站
2022-07-04 10:20:00
...
好久不用,忘了 记一下
//get data from db
List<ModelClass> source = new List<ModelClass>();
//单一条件分组
var sc1 = from sf in source
group sf by sf.Name;
//多个条件分组
var sc2 = from sf in source
group sf by new { sf.Name,sf.Price,sf.Amount}
into g
select new
{
Name= g.Key.Name,
Price= g.Sum(p=>p.Price),
Amount = g.Sum(p=>p.Amount)
};
//多个条件分组,并返回新的对象
var sc3 = from sf in source
group sf by new { sf.Name }
into g
//select new { Name = g.Key.Name, Price = g.Sum(p => p.Price), Amount = g.Sum(p => p.Amount) };
select new ModelClass()
{
Name = g.Key.Name,
Price =g.Sum(p=>p.Price),
Number = g.Sum(p=>p.Number),
};
上一篇: swiper 3D 覆盖流的使用方法
下一篇: C# linq group by的运用