【发布时间】:2015-09-04 08:27:16
【问题描述】:
我想获得一个客户集合,包括几个属性,其中是地址,但只有在它尚未被删除时 (SuppressionDate == null)
IQueryable<Customer> customers =
context.Persons.OfType<Customer>()
.Include(customer => customer.Addresses)
.Include(customer => customer.Bills)
.Include(customer => customer.Code)
.Include(customer => customer.Tutors);
我尝试了几种方法来使用 where 子句来过滤地址:
...
.Include(customer => customer.Addresses.Where(a => a.SuppressionDate == null))
.Include(customer => customer.Bills)
...
这是我的第一次尝试,但它引发了以下异常:
System.ArgumentException : 包含路径表达式必须引用 在类型上定义的导航属性。使用虚线路径 参考导航属性和用于集合的 Select 运算符 导航属性。参数名称:路径
我也尝试在 Include() 末尾和查询末尾使用相同的 where 子句,但似乎都不起作用。
我目前正在使用一种解决方法,它遍历客户集合并删除被删除的地址:
foreach(Customer c in customers){
customer.Addresses = customer.Addresses.Where(a => a.SuppressionDate == null).ToList();
}
对 linq to object/entities 相当陌生,我想知道是否有内置方法可以实现这一点。
【问题讨论】:
-
返回的异常是什么?
-
尝试查看msdn.microsoft.com/en-gb/data/jj574232.aspx#explicitFilter,它提供了有关该类型过滤的一些信息
标签: c# linq-to-entities where