C# 如何过滤IQueryable整棵树?
我有C# 如何过滤IQueryable整棵树?,c#,where,iqueryable,C#,Where,Iqueryable,我有联系人查询,其中包含通信子查询,其中包含通信扩展子查询 static class Program { public class Contact { public int ContactID { get; set; } public List<Communication> Communications { get; set; } public DateTime? Dele
联系人
查询,其中包含通信
子查询,其中包含通信扩展
子查询
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();
}
}
静态类程序
{
公共类联系人
{
public int ContactID{get;set;}
公共列表通信{get;set;}
公共日期时间?删除日期{get;set;}
}
公共类通信
{
公共int通信ID{get;set;}
公共列表通信扩展{get;set;}
公共日期时间?删除日期{get;set;}
}
公共类通信扩展
{
公共int通信扩展ID{get;set;}
公共整数区域代码{get;set;}
公共日期时间?删除日期{get;set;}
}
静态void Main(字符串[]参数)
{
IQueryable q=生成数据();
可预测的结果=
q、 其中(c=>c.DeletionDate==null&&
c、 通信。其中(co=>co.DeletionDate==null&&
其中(ce=>ce.DeletionDate==null));
}
私有静态IQueryable GenerateData()
{
返回新列表
{
新联系人
{
ContactID=1,
DeletionDate=DateTime.Now,
通讯=
新名单
{
新通信
{
CommunicationId=1,
DeletionDate=DateTime.Now,
通信扩展=
新名单
{
新通信扩展
{
CommunicationExtensionID=1,
区号=5,
DeletionDate=null
}
}
},
新通信
{
通讯ID=2,
DeletionDate=null,
通信扩展=
新名单
{
新通信扩展
{
CommunicationExtensionID=2,
区域代码=55,
DeletionDate=DateTime.Now
}
}
}
}
},
新联系人
{
ContactID=2,
DeletionDate=null,
通讯=
新名单
{
新通信
{
CommunicationId=1,
DeletionDate=null,
通信扩展=
新名单
{
新通信扩展
{
CommunicationExtensionID=3,
区号=54,
DeletionDate=null
}
}
},
新通信
{
通讯ID=2,
DeletionDate=DateTime.Now,
通信扩展=
新名单
{
新通信扩展
IQueryable<Contact> result =
q.Where(c => c.DeletionDate == null &&
c.Communications.Where(co => co.DeletionDate == null &&
co.CommunicationExtensions.Where(ce => ce.DeletionDate == null)));
c.Communications.Where(co => co.DeletionDate == null &&
co.CommunicationExtensions.Where(ce => ce.DeletionDate == null)