首页 > 学院 > 开发设计 > 正文

EF GroupBy多个字段

2019-11-10 18:31:26
字体:
来源:转载
供稿:网友

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

更多参考:

关于EF上线文异常问题整理

TransactionScope线程安全问题整理

EF日期格式筛选


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表