2012-01-20 1 views
2

작동하지 않음을 선택합니다 (많은)에 포함 : Document의 (a Customer을 가지고)와 Customer을 중 하나를 기반으로 고객에 대한 문서를 얻을 수RIA 서비스 : 나는 두 엔티티 유형이

내 쿼리는 (모음 Documents있다) 고객의 이름 또는 번호

쿼리는 다음과 같습니다

public IQueryable<Document> GetCustomerDocuments(DateTime startDate, DateTime endDate, string filterText) 
{ 
    return this.ObjectContext.Customers 
       .Where(c => c.CustomerName.Contains(filterText) || c.CustomerNumber.Contains(filterText)) 
       .SelectMany(c => c.Documents) 
       .Where(d => d.Date >= startDate && d.Date <= endDate); 
} 

쿼리 반환, 나는 그것이 둘다 DocumentCustomer 엔티티를 포함 할 .... 제가 포함 생각할 수있는 모든 것을 시도

Include("Documents.Customer"), Include("Customer") 등.

나는 확실히 IncludeAttribute을 메타 데이터로 설정했습니다.

생각하십니까? 이것은 가능한가?

감사합니다.

답변

4

, 내가 사용하여 LINQ 쿼리를 쓴 가입 :

 var v = from cust in (from c in this.ObjectContext.Customers 
       where (c.CustomerName.Contains(filterText) || c.CustomerNumber.Contains(filterText)) select c) 
       join doc in this.ObjectContext.Documents on cust.CustomerNumber equals doc.CustomerNumber 
       where doc.Date >= startDate && doc.Date <= endDate 
       select doc; 
     return ((ObjectQuery<Document>)v).Include("Customer").AsQueryable<Document>(); 

이렇게하면 문제가 해결됩니다!

+0

쿼리 결과를 ObjectQuery로 캐스팅하는 절대 생명의 은인. 답으로 표시하십시오. –

0

.Include은 ObjectQuery에서 작동하지 않으며 사용자 정의 투영을 추가 할 때 효과가 취소됩니다.

return this.ObjectContext.Documents.Include("Customers") 
      .Where(d => d.Customers.Any(c => 
             c.CustomerName.Contains(filterText) 
            || c.CustomerNumber.Contains(filterText)) 
      .Where(d => d.Date >= startDate && d.Date <= endDate); 

이것은 또는 작동하지 않을 수 있으며, 수도 있고 알맞은 SQL을 생성 할 수 없습니다; : 문서의 측면에서

가 쿼리를 다시 작성

다음과 같은 옵션을 시도 할 수 있습니다 테스트해야합니다.

또 다른 가능성은 DTO 객체를 정의하는 것입니다

class CustomerDocument 
{ 
    public Customer {get;set;} 
    public Document {get;set;} 
} 

그런 다음 쿼리가된다 :

대신 투사와 SelectMany를 사용
return from c in this.ObjectContext.Customers 
     from d in c.Documents 
     where (c.CustomerName.Contains(filterText) 
      || c.CustomerNumber.Contains(filterText)) 
      && d.Date >= startDate && d.Date <= endDate 
     select new CustomerDocument {Customer = c, Document = d}; 
+1

그건 내가 두려워했던거야. 처음에는 원래 방식대로하고 있었지만,'Documents '를 거쳐'Customer'에 도착해야만 너무 느려지므로 뒤집기 위해 노력하고있었습니다. DTO 객체를 생각조차하지 못했습니다. 훌륭한 아이디어입니다. (그것이 고객 정보를 복제하게 될지도 모르지만, 맞습니까?) – nosirrahcd

+1

아마 그렇습니다. RIA 서비스 인프라가 어쨌든 그것을 처리하지 않는 한, 그렇지만 의심 스럽습니다 (반환 된 목록의 동일한 ID를 가진 2 명의 고객이 동일한 경우 클라이언트 측을 검증하여 테스트 할 수 있습니다). – jeroenh