user2994682
user2994682

Reputation: 503

C#, Lambda to transfer elements from one list to another

Is there a simple lambda expression to extract elements from one list and put them into another? without LINQ?

for example to map, a source list of elements T to another list (or return a list) with the string name for each element in the source.

Update...with pseudocode.

List<int> intList = new List<int>() { 1, 2, 3};         
List<string> stringList = new List<string>(intList.ToArray((i) => intList[i].ToString())); // this doesn't work obviously

stringList should be {"1", "2", "3"}

Upvotes: 0

Views: 664

Answers (1)

dana
dana

Reputation: 18155

List<T>.ConvertAll() provides a straightforward way to change types without LINQ.

In your case...

List<string> stringList = intList.ConvertAll(i => i.ToString());

Upvotes: 7

Related Questions