【发布时间】:2016-05-20 14:28:19
【问题描述】:
我想按名称和数量对我的数据进行分组,返回的每个组都必须有一个唯一的 ID,该 ID 在数据中每组递增。
这是相关的代码:
return JsonConvert.SerializeObject(
data.GroupBy(x=> new {x.Name, x.Amount})
.Where(gp => gp.Count() > 1)
.Select(
gp =>
new Group
{
GroupData = gp.Select(el => new GroupItem
{
Name = el.Name,
Amount = el.Amount,
GroupId = <missing>
})
}));
我发现了几个关于为组中的每一行添加增量 ID 的问题,每个组都会重置,但是我希望组是这样的:
Group = {"bob", 145.20, 1},
{"bob", 145.20, 1},
Group = {"steve", 120.00, 2},
{"steve", 120.00, 2}..... etc
我找到的唯一示例是在 SQL 中执行此操作,而在 C# 中我需要在 LINQ 中执行此操作。
【问题讨论】: