EF的GroupBy多字段分组查询GroupBy根据多个字段分组使⽤⽅式:
⼀、使⽤扩展⽅法
query.GroupBy(q => new { q.Year, q.Month })
.Select(q => new
{
Year = q.Key.Year,
Month = q.Key.Month,
BuildAmount = q.Sum(i => i.BuildAmount),
RecAmount = q.Sum(i => i.RecAmount),
Amount = q.Sum(i => i.Amount),
RealAmount = q.Sum(i => i.RealAmount)
});
⼆、Linq⽅式
var query = from l in list
group l by new { l.Name, l.BatNum } into g
groupby分组select new
{
Name = g.Key.Name,
Count = g.Sum(a => a.Count),
BatNum = g.Key.BatNum
};
三、⽣成KeyValue类型
Dictionary<string,decimal> buildList = _build.FindList().GroupBy(q => q.SaleCode)
.Select(q => new { Code = q.Key, ReaAmount = q.Sum(i => i.RealAmount) })
.ToDictionary(q => q.Code, q => q.ReaAmount);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。