2012-05-30 2 views
-1

임 ....linq 투영 및 프로젝트를 루트 투영 된 객체의 하위 컬렉션에 어떻게 사용합니까? 이 작업을 수행하려고

var entity = landingPages.Select(x = new MyClass { name = x.name, age = x.age }); 

유일한 문제는 ... 나이는 실제로 coolection 그래서 내가 이런 짓을 했을까 생각입니다

var entity = landingPages.Select(x = new MyClass { name = x.name, age.add(x.age) }); 

입니다 그러나 그것은 일을 해달라고. 이것이 가능한가? 당신은 모음 (LandingPage의 연령 MyClass에 '나이)이었다 나이 말하지 않았다

+0

왜 작동하지 않는 이유는 무엇입니까? – hwcverwe

+0

age.add (x)를 할 수 없기 때문에 가능합니까? 그게 내 질문 .... – Exitos

+0

뜻 : 당신이 얻을 컴파일/런타임 오류가 무엇입니까 – hwcverwe

답변

2
var entity = landingPages.Select(x => new MyClass 
{ //this is an initialization block. Only Property Assignment is allowed. 
    name = x.name, 
    age.add(x.age) //arbitrary statements are not allowed in initialization blocks. 
}); 

.

LandingPage의 나이라면 다음을 수행하십시오. 단일 값을 콜렉션으로 변환하십시오.

IEnumerable<MyClass> query = landingPages.Select(x => new MyClass 
{ 
    name = x.name, 
    age = new List<int>() { x.age } //this is a collection initializer 
}); 

MyClass '나이 인 경우 : 컬렉션을 단일 값으로 변환하십시오.

IEnumerable<MyClass> query = landingPages.Select(x => new MyClass 
{ 
    name = x.name, 
    age = x.age.Sum() 
}); 
+0

데이비드 이것이 내가 찾고 있던 .... 감사합니다 Logged – Exitos