HerbalMart
HerbalMart

Reputation: 1729

.NET Linq. Confusion about SelectMany

In the given code example, why do we use x.ToArray() ? Each element x is already an array isn't it ?

Please make me less confused :)

    var array1 = new int[3] { 1, 2, 3 };     //New integer array
    var array2 = new int[3] { 4, 5, 6 };     //New integer array
    var array3 = new int[3] { 7, 8, 9 };     //New integer array

    IList<int[]> list4 = new List<int[]> { array1, array2, array3 };

    var theManyList = list4.SelectMany(x => x.ToArray()).ToList();

Upvotes: 2

Views: 767

Answers (1)

pstrjds
pstrjds

Reputation: 17438

You don't need it. You can just do:

list4.SelectMany(x => x).ToList();

The reason is exactly as you stated it, the arrays are already arrays. SelectMany takes an IEnumerable<T>, so no need to add the extra operation. Why someone did that in an example, don't know. Maybe they were trying to make it clear that you had to pass an IEnumerable?

Upvotes: 6

Related Questions