2011-04-06 7 views
1

모든 하위 목록이 필요합니다. 아이 항목 만 가져 오면 어떻게 될까요? 내가linq 쿼리를 사용하여 모든 하위 항목을 얻는 방법

//lets get the parents along with the childs 
var parents = from p in context.post 
       where p.isDeleted == false && p.userid == new Guid(UserID) 
        let relatedchilds = from c in context.post 
             where c.id == p.id 
             select c.id 
        select new 
        { 
         p.id, 
         p.userid, 
         relatedchilds 
        } 

//now lets get only the childs in the previous query 
var childs = from c in parents 
       select new 
       { 
        c.relatedchilds. //This is where my problem is 
       } 

가 어떻게 하나 개의 컬럼만을 relatedchilds받을 수 있나요 차일을 얼마나 여기

은?

var childs = parents.SelectMany(p => p.relatedchilds); 

또는 쿼리 형식 : relatedchilds 이후

답변

4

이미 콜렉션 자체는 각 부모의 내부의 모음입니다, 당신은 아이디의의 평평한 모음으로 중첩 된 컬렉션을 평평하게 SelectMany()을 사용할 수 있습니다

var childs = from p in parents 
      from child in p.relatedchilds 
      select child; 
+0

와우 .. 감사합니다. – Luke101

관련 문제