我有一个具有以下结构的项目列表:
如何根据Id
字段的Date
和日期对其进行分组(日期时间)并具有该组的总权重?我想要这样的东西:
答案 0 :(得分: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)});
答案 1 :(得分: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)
};