我有结构:
List<List<x>> structure = new { {x,x}, {x,x,x,x}, {x,x,x}}
如何使用linq将其投影到以下序列?
{1,1},{2,1},{3,2},{4,2},{5,2},{6,2},{7,3},{8,3},{9,3}
因此,结果元素的第一个属性必须表示基元素的全局索引,第二个属性必须表示该元素所属的组的索引。
示例:第三组的第二元素将预测为{8,3}:
8-基本元素的全球指数
3-群基元素的指数属于。
发布于 2016-10-27 12:58:48
您可以通过使用包含索引的Select和SelectMany版本来做到这一点。
IList<IList<int>> structure = new[]
{
new[] { 1, 1 },
new[] { 1, 1, 1, 1 },
new[] { 1, 1, 1 }
};
var result = structure.SelectMany((l, i) => l.Select(v => i))
.Select((i, j) => new[] {j + 1, i + 1});
Console.WriteLine(string.Join(",", result.Select(l => "{" + string.Join(",", l) + "}")));输出
{1,1},{2,1},{3,2},{4,2},{5,2},{6,2},{7,3},{8,3},{9,3}
https://stackoverflow.com/questions/40284968
复制相似问题