构建从 linq 到 ef core 的动态 where 查询
我的模型如下所示
public class Country
{
public int Id {get; set;}
public string Name {get; set;}
}
public class User
{
public int Id{get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
}
public class Company
{
public int Id{get; set;}
public string Name {get;set;}
public string City {get; set;}
public string Address1 {get; set;}
public string Address2 {get; set;}
[ForeignKey("Country")]
public int CountryId{get; set;}
public Country Country{get; set;}
public ICollection<CompanyUsers> CompanyUsers {get; set;}
}
public class CompanyUsers
{
public int Id{get; set;}
[ForeignKye("Company")]
public int CompanyId {get; set;}
[ForeignKye("User")]
public int UserId {get; set;}
public Country Country{get; set;}
public User User{get; set;}
}
我希望让用户能够通过国家、公司或用户类
中定义的任何属性搜索公司记录除了 Id 属性
我尝试查看这个SO讨论但它不处理导航属性。有人可以帮助我构建动态 where 子句,包括导航属性中的属性。
通过这个实现我只能做如下
myContext.CompanyEntity
.Where(FilterLinq<CompanyEntity>
.GetWherePredicate(searchParams))
.ToList();
public class SearchField
{
public string Key { get; set; }
public string Value { get; set; }
}
My model looks like below
public class Country
{
public int Id {get; set;}
public string Name {get; set;}
}
public class User
{
public int Id{get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
}
public class Company
{
public int Id{get; set;}
public string Name {get;set;}
public string City {get; set;}
public string Address1 {get; set;}
public string Address2 {get; set;}
[ForeignKey("Country")]
public int CountryId{get; set;}
public Country Country{get; set;}
public ICollection<CompanyUsers> CompanyUsers {get; set;}
}
public class CompanyUsers
{
public int Id{get; set;}
[ForeignKye("Company")]
public int CompanyId {get; set;}
[ForeignKye("User")]
public int UserId {get; set;}
public Country Country{get; set;}
public User User{get; set;}
}
I want to let user to be able to search Company record either by any property defined in Country, Company or User class
except Id property
I tried looking at this SO discussion but it doesn't handle navigation property. Could someone help me to build dynamic where clause including properties from navigation property.
With this implementation I can only do as below
myContext.CompanyEntity
.Where(FilterLinq<CompanyEntity>
.GetWherePredicate(searchParams))
.ToList();
public class SearchField
{
public string Key { get; set; }
public string Value { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您指出的答案中扩展这个想法并不那么复杂。
您需要更改假设,以便 SerchField 中的属性名称可以包含属性路径的定义,例如
Company.City
或Company.Country.Name
以及属性FirstName
。并且您需要处理它:,而不仅仅是像这样简单的属性
因此,您需要处理属性链
:基本上这段代码的作用是在创建属性链的每个循环上将属性应用于之前修改的
pe
变量。实体框架将处理它并创建适当的连接。这仅适用于单一关系,不适用于集合。所以该答案的修改后的代码如下所示:
Expanding on the idea from answer you pointed to is not that complicated.
You need to change assumption so the property name in SerchField can hold definition of property path like
Company.City
orCompany.Country.Name
as well as just propertyFirstName
. And you need to handle it:so instead of just having simple property like so:
you need to handle property chain:
Basically what this code does it applies properties over previous modifying
pe
variable on every loop creating property chain. Entity framework will process it and create appropriate joins. This will work only for single relations and not for collections.so the modified code from that answer looks like this: