0

我有Contact包含Communication子查询的查询,其中包含CommunicationExtension子查询

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();
        }
    }

当我尝试构建它时,我收到错误:

运算符“&&”不能应用于“bool”和“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 个表,每个表都包含可为空的字段DeletionDate

4

1 回答 1

1

这是问题所在:

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

这两行不返回布尔值,因此您的Where调用不知道如何处理它。您是否想要任何至少有一个未删除Communications和的联系人CommuincationExtensions?如果是这样,那么将Where调用更改为Any它应该可以工作。但是,请注意,在实际与您打交道时,您Contacts仍然需要过滤掉任何已删除的Communications/CommunicationExtensions因为无论如何关系只会为您提供相关记录。换句话说,contact.Communications将返回 allCommunications与 that 相关的Contactcomm.CommunicationExtensions并将返回 all CommunicationExtensions,因此您仍然需要在处理它们时过滤掉任何已删除的记录。

于 2012-05-31T13:40:09.310 回答