如何使用linq计数和分组

时间:2017-12-22 06:25:29

标签: c# linq group-by count

我想按位置ID和sdmaterial计算生产和分组,但是它的错误

Prodution = productionLala.Where(n => n.Date >= x.StartDate && n.Date < x.EndDate)
                          .GroupBy(n => n.From_Location_ID && n.SDMaterial_Group)
                          .Select(n.production).Count();
有人能帮帮我吗? :) thx

2 个答案:

答案 0 :(得分:1)

尝试这可能会有效 -

var Prodution = productionLala.Where(n => n.Date >= x.StartDate && n.Date < 
x.EndDate).GroupBy(x => new{ x.From_Location_ID , x.SDMaterial_Group})
.Select(n.production).count();

答案 1 :(得分:1)

是否要为CountFrom_Location_ID检索SDMaterial_Group

var groupCounts = productionLala.Where(n => n.Date >= x.StartDate && n.Date < x.EndDate)
    .GroupBy(x => new
    {
        x.From_Location_ID,
        x.SDMaterial_Group
    })
    .Select(x => new
    {
        From_Location_ID = x.Key.From_Location_ID,
        SDMaterial_Group = x.Key.SDMaterial_Group,
        Count = x.Count()
    });