2012-05-31 2 views
0

내가, 내가 오류를 내가 그것을 구축 할 때 CommunicationExtension 하위 쿼리IQueryable 전체 트리를 필터링하는 방법?

static class Program 
    { 
     public class Contact 
     { 
      public int ContactID { get; set; } 
      public List<Communication> Communications { get; set; } 
      public DateTime? DeletionDate { get; set; } 
     } 

     public class Communication 
     { 
      public int CommuncationID { get; set; } 
      public List<CommunicationExtension> CommunicationExtensions { get; set; } 
      public DateTime? DeletionDate { get; set; } 
     } 

     public class CommunicationExtension 
     { 
      public int CommunicationExtensionID { get; set; } 
      public int AreaCode { get; set; } 
      public DateTime? DeletionDate { get; set; } 
     } 

     static void Main(string[] args) 
     { 
      IQueryable<Contact> q = GenerateData(); 
      IQueryable<Contact> result = 
       q.Where(c => c.DeletionDate == null && 
        c.Communications.Where(co => co.DeletionDate == null && 
         co.CommunicationExtensions.Where(ce => ce.DeletionDate == null)); 
     } 



     private static IQueryable<Contact> GenerateData() 
     { 
      return new List<Contact> 
         { 
          new Contact 
           { 
            ContactID = 1, 
            DeletionDate = DateTime.Now, 
            Communications = 
             new List<Communication> 
              { 
               new Communication 
                { 
                 CommuncationID = 1, 
                 DeletionDate = DateTime.Now, 
                 CommunicationExtensions = 
                 new List<CommunicationExtension> 
                  { 
                   new CommunicationExtension 
                    { 
                     CommunicationExtensionID = 1, 
                     AreaCode = 5, 
                     DeletionDate = null 
                    } 
                  } 
                }, 
               new Communication 
                { 
                 CommuncationID = 2, 
                 DeletionDate = null, 
                 CommunicationExtensions = 
                 new List<CommunicationExtension> 
                  { 
                   new CommunicationExtension 
                    { 
                     CommunicationExtensionID = 2, 
                     AreaCode = 55, 
                     DeletionDate = DateTime.Now 
                    } 
                  } 
                } 
              } 
           }, 
          new Contact 
           { 
            ContactID = 2, 
            DeletionDate = null, 
            Communications = 
             new List<Communication> 
              { 
               new Communication 
                { 
                 CommuncationID = 1, 
                 DeletionDate = null, 
                 CommunicationExtensions = 
                 new List<CommunicationExtension> 
                  { 
                   new CommunicationExtension 
                    { 
                     CommunicationExtensionID = 3, 
                     AreaCode = 54, 
                     DeletionDate = null 
                    } 
                  } 
                }, 
               new Communication 
                { 
                 CommuncationID = 2, 
                 DeletionDate = DateTime.Now, 
                 CommunicationExtensions = 
                 new List<CommunicationExtension> 
                  { 
                   new CommunicationExtension 
                    { 
                     CommunicationExtensionID = 4, 
                     AreaCode = 5565, 
                     DeletionDate = null 
                    } 
                  } 
                } 
              } 
           } 
         }.AsQueryable(); 
     } 
    } 

을 포함 Communication 하위 쿼리가 포함 Contact 쿼리를 얻을 수있다 :

운영자에 적용 할 수 없습니다 '&가 &' 라인 형 'System.Collections.Generic.IEnumerable' '부울'와

의 피연산자 :

IQueryable<Contact> result = 
       q.Where(c => c.DeletionDate == null && 
        c.Communications.Where(co => co.DeletionDate == null && 
         co.CommunicationExtensions.Where(ce => ce.DeletionDate == null))); 

삭제되지 않은 모든 데이터를 필터링해야합니다 (DeletionDate == null). 내 시나리오에서는 데이터베이스 ~ 200 개 테이블이 각 테이블이 여기에 널 (NULL) 필드 DeletionDate

답변

1

을 포함하는 문제입니다 : 그 두 선이 부울을 반환하지 않는

c.Communications.Where(co => co.DeletionDate == null && 
    co.CommunicationExtensions.Where(ce => ce.DeletionDate == null) 

, 그래서 당신의 Where 전화는 알 수 없습니다 그것으로 무엇을해야하는지. 적어도 하나의 삭제되지 않은 연락처가 CommunicationsCommuincationExtensions 인 연락처를 원하십니까? 그렇다면 Where 전화를 Any으로 변경하면 제대로 작동합니다. 그러나 실제로 Contacts을 처리 할 때 어떤 관계가 있더라도 상관 관계가 표시되므로 삭제 된 Communications/CommunicationExtensions을 필터링해야합니다. 즉 contact.CommunicationsContact과 관련된 모든 Communications을 반환하고 comm.CommunicationExtensions은 모두 CommunicationExtensions을 반환하므로 삭제 된 레코드는 처리 할 때마다 필터링해야합니다.

+0

삭제되지 않은 모든'Contacts'''''''''와'CommunicationExtensions''을 원합니다. 이러한 관련 테이블은이 예에서와 같이 3 개를 초과 할 수 있습니다. 그리고 여러 개의'Where()'에 이것을 써야합니다. 불필요한 모든 데이터를로드하기 때문에'ToList()'를 사용할 수 없습니다. 나중에이 데이터는 단일 '연락처'를보기 위해 바인드됩니다. '통신'또는 'CommunicationExtensions'에서 필요로 할 때 더 많은 데이터가로드됩니다. –

관련 문제