views:

164

answers:

1

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

+11  A: 

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();
JaredPar