[Solved] Convert IDs from List to string[]


Use projection in LINQ:

var ids = myClassList.Select(myClass => myClass.ID).ToArray();

Under using System.Linq;

The Select extension method allows you to “project” a type into something else (including anonymous types). With the new type inference mechanisms in the compiler you don’t even need to specify the generic arguments for Select.

The Select will return an IEnumerable<string> in this case.

ToArray does as it says on the tin, converts the IEnumerable<T> into a T[].

Projection is just fancy terminology for a subset of Linq methods, the work is done by your own code (in the above case this is the lambda expression: myClass => myClass.ID). The Select method takes a Func<TSource, TResult>, and whatever your code returns will be what TResult becomes, in this case we return a string property of MyClass so you get a string.

0

solved Convert IDs from List to string[]