1

I am using the SelectMany method to create a combination of items from two list of strings. I am able to create the flattended list without any issues, but unable to figure out how to add the index. In the example below, I need to assign the Position property of Product with the Index.

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});

Thanks for any help,

4

1 回答 1

2

您在错误的位置指定索引。之后你需要它SelectMany。例如:

var products = strings.SelectMany(x => suffixex,
                                  (x, y) => x + y)
                      .Select((id, index) => new Product { Position = index,
                                                           ID = id });
于 2012-08-09T20:40:54.243 回答