List <T> for string []

Using C #, I have a list of Foo types that has a string property string. I would like to convert this list to an array of strings using the Bar property.

Is there an easy way (LINQ?) To do this without having to scroll through the collection?

+3
source share
2 answers
 List<Foo> l = GetMyList();
 string[] myStrings = l.Select(i => i.Bar).ToArray();

Note that, like all linq code, this still goes through the collection - you just don't write the loop yourself.

Also note that you should avoid calling .ToArray () until the last moment. Are you sure IEnumerable won't be enough here?

+12
source

Try the following:

 string[] bars = myList.Select(x => x.Bar).ToArray();
+4
source

Source: https://habr.com/ru/post/1769660/


All Articles