Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# collapse array list

if i have

class foo
{ 
   int a
   int b
}

and a List<foo> myList

is there some short hand notation for to make a List<int> from eg myList[*].a, ie pick out a from each element and making a new list

clearly this can be done by iterating through myList, but seems to happen often and i was wondering if there's a shortcut notation

same question for array etc

thanks

like image 676
second Avatar asked Sep 22 '26 23:09

second


1 Answers

If you are using the C# 3.0 or higher compiler (VS2008 or up), try the following

List<Foo> list = GetTheList();
List<int> other = list.Select(x => x.a).ToList();
like image 197
JaredPar Avatar answered Sep 25 '26 13:09

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!