我有以下有效的 LINQ 语句
var separator = new[] { " ||| " };
var objectives = data.Select(s => s.Split(separator, StringSplitOptions.RemoveEmptyEntries))
.GroupBy(strings => strings[0])
.Select(grouping => new Objective
{
Text = grouping.Key,
ObjectiveDetails = grouping.Select(s => new ObjectiveDetail {
Text = s[1]
}).ToList()
})
.ToArray();
return objectives;
}
我想做的是在 Objective 对象内填充一个名为 Number 的字段,并在 ObjectiveDetail 对象内填充一个名为 Number 的字段。我希望数字从 1 开始并递增,以便内部的每个新 Objective 和 ObjectiveDetail 都获得一个分配给它的新数字。我尝试执行以下操作:
var separator = new[] { " ||| " };
var objectives = data.Select(s => s.Split(separator, StringSplitOptions.RemoveEmptyEntries))
.GroupBy(strings => strings[0])
.Select(grouping, index => new Objective
{
Number = grouping.Index,
Text = grouping.Key,
ObjectiveDetails = grouping.Select(s, index => new ObjectiveDetail {
Number = index,
Text = s[1]
}).ToList()
})
.ToArray();
return objectives;
}
这给了我一个错误 .Select(grouping, 行说“名称分组不存在”。有人可以帮助告诉我如何解决这个问题并向 GroupBy 添加索引吗?