【发布时间】:2018-07-25 21:59:00
【问题描述】:
在我的Customer 模型中,我有
public virtual ICollection<AddressModel> AddressIDs { get; set; }
引用AddressModel 为我提供客户与其地址之间的一对多关系。
我有一个搜索功能,它使用
var CustomerList = _context.Customers
.Where(ps => ps.Surname.Contains(surnameToSearchFor))
按姓氏限制返回的数据集。
我正在尝试添加在地址中搜索邮政编码的功能。按照各种链接,这在 Visual Studio 中有效,但在执行时中断
CustomerList = CustomerList
.Include(ps => ps.AddressIDs
.Where(a => a.Postcode == postcodeToSearchFor));
有错误
InvalidOperationException: The property expression 'ps => {from AddressModel a in ps.AddressIDs where ([a].Postcode == __p_0) select [a]}' is not valid. The expression should represent a property access: 't => t.MyProperty
如何在子表上的 LINQ 中添加Where 子句?
编辑 对于建议Multiple WHERE clause in Linq 作为答案的人,该问题显然与单个表有关,而我明确询问了子表。
【问题讨论】:
-
使用内置方法无法做到这一点。看起来这是一个已知问题,并且有一个扩展库:Entity Framework Plus | IncludeFilter
标签: c# linq entity-framework-core