使用selectmany查询中的索引进行Linq
我使用SelectMany方法从两个字符串列表中创建项目组合。我能够创建扁平化列表,没有任何问题,但无法确定如何添加索引。在下面的示例中,我需要使用索引指定Product的Position属性使用selectmany查询中的索引进行Linq,linq,Linq,我使用SelectMany方法从两个字符串列表中创建项目组合。我能够创建扁平化列表,没有任何问题,但无法确定如何添加索引。在下面的示例中,我需要使用索引指定Product的Position属性 var strings = new List<string> { "Milk", "Eggs", "Cheese" }; var suffixes = new List<string> {"-Direct", "-InDirect"}; var products = string
var strings = new List<string> { "Milk", "Eggs", "Cheese" };
var suffixes = new List<string> {"-Direct", "-InDirect"};
var products = strings
.SelectMany((_, index) => suffixes,
(x, y) => new Product {Position = ?, ID = x + y});
var strings=新列表{“牛奶”、“鸡蛋”、“奶酪”};
变量后缀=新列表{“-Direct”,“-InDirect”};
var乘积=字符串
.SelectMany(u,index)=>后缀,
(x,y)=>新产品{Position=?,ID=x+y});
感谢您的帮助,您在错误的位置指定了索引。在
选择many
之后,您需要它。例如:
var products = strings.SelectMany(x => suffixex,
(x, y) => x + y)
.Select((id, index) => new Product { Position = index,
ID = id });
哪个索引-字符串中的索引(0-2)或最终索引(0-5)?@JonSkeet Jon,我正在寻找最终索引0-5是否完美。效果很好。谢谢你,乔恩。