我有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