2010-02-24 4 views
4

아래 쿼리를 사용하여 여러 조인을 수행하고 모두 선택합니다. 이 결과를 목록으로 반환하고 싶습니다.이 경우에는 3 개의 수를 가진 List가 있습니다.이 단일 고객 ID 순서와 연관된 세 개의 주소가 있다고 가정합니다. 지금 쿼리가 작동하지만 exp .ToList()는 필자에게 본질적으로 2 차원 목록 (이 요소가 3 요소의 유형 목록 인 단일 요소가있는 목록)을 제공합니다.이 작업을 수행하는 좋은 방법이 있다고 생각합니다. 생각 ...하나의 목록으로 여러 조인

var exp = (

     from t in this.reposOrders.All() 

     join p1 in this.reposAddress.All() 
     on t.AddressPrimary equals p1.AddressID into pp1 
     from p1 in pp1.DefaultIfEmpty() 

     join p2 in this.reposAddress.All() 
     on t.AddressSecondary equals p2.AddressID into pp2 
     from p2 in pp2.DefaultIfEmpty() 

     join p3 in this.reposAddress.All() 
     on t.AddressThird equals p3.AddressID into pp3 
     from p3 in pp3.DefaultIfEmpty() 

     where t.CustomerID == customerID 

     select new { p1, p2, p3 } 
    ); 
?

답변

0

싶은 것은 가난한 정상화 앓고, AddressPrimary는 AddressSecondary 및 AddressThird (나는 요에서 순서를 믿고있어 주문의 필드 안 ur line from t in this.reposOrders.All()) 약한 엔티티 또는 조인 테이블에 포함되어야합니다. 정상화 당신의 비판

var primary = from t in this.reposOrders.All() where t.CustomerID == customerID select t.AddressPrimary; 
var secondary = from t in this.reposOrders.All() where t.CustomerID == customerID select t.AddressSecondary; 
var tertiary = from t in this.reposOrders.All() where t.CustomerID == customerID select t.AddressThird; 
var ids = primary.Union(secondary).Union(tertiary); 
var addresses = from a in this.reposAddress.All() where ids.Contains(a.AddressID) select a; 
+0

감사합니다,하지만 난 동의 :

당신은 아마 당신이 다음 쿼리와 함께 원하는 것을 얻을 수 말했다. – Gabe

+0

그건 내 부분에서 잘못 말한거야, 미안. 조인 테이블을 사용하는 것이 더 좋을 것이라고 생각합니다. 레이아웃 한 방법을 시도해 보셨습니까? 본질적으로 1 차, 2 차 및 3 차 주소를 하나의 ID 목록으로 축소 한 다음 원하는 주소를 얻기 위해 질의합니다. LINQ는 지연 실행이므로 ToList()를 호출 할 때까지 실제로 반복하지 않습니다. – Randolpho

관련 문제