문제

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

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top