0

我有一个具有以下结构的项目列表:

  1. ID
  2. 约会时间
  3. 重量

如何按字段日期(即 DateTime )对它们进行分组获得Id组的总权重?我想要这样的东西:Date

  1. ID
  2. 日期
  3. 总重量
4

2 回答 2

3

如果你想用 ID 和 Date 对它们进行分组,你可以这样做:

var grp = from x in items
          group by new {x.Id, x.DateTime.Date};

var result = grp.Select(x=>new {item = x.Key, TotalWeight = x.Sum(y=>y.Weight)});
于 2012-05-22T18:30:22.727 回答
0
var result = from x in items
             group x by new { x.Id, x.DateTime.Date } into grouping
             select new 
             { 
                 id = grouping.Key.Id, 
                 Date = grouping.Key.Date, 
                 TotalWeight = grouping.Sum(y=>y.Weight)
             };
于 2012-05-22T19:11:54.427 回答