LINQ 查询动态过滤数据库,其中子表列等于可选输入值
因此,我有一种方法可以根据用户的可选输入动态更改 LINQ 查询。
var query = from i in _dbContext.Incidents.Include(Incident => Incident.childtable1)
.Include(Incident => Incident.childtable2)
.Include(Incident => Incident.childtable3)
.Include(Incident => Incident.childtable4)
select i;
// Append Query to take into account user selection
query = query.
if (bool)
query = query.Where(i => i.column.Equals(userinput));
if (bool)
query = query.Where(i => i.column1.Equals(userinput1));
if (bool)
query = query.Where(i =>i.column2.Equals(userinput2));
var results = query.ToList();
return results;
}
但是,我还希望能够过滤用户输入与子表之一中的列匹配的结果。
例如,
if (bool)
query = query.Where(i =>i.childtable2.column2.Equals(userinput2));
有没有办法将其添加到我当前的方法中?
So I have a method to dynamically alter a LINQ query based on optional inputs from the user.
var query = from i in _dbContext.Incidents.Include(Incident => Incident.childtable1)
.Include(Incident => Incident.childtable2)
.Include(Incident => Incident.childtable3)
.Include(Incident => Incident.childtable4)
select i;
// Append Query to take into account user selection
query = query.
if (bool)
query = query.Where(i => i.column.Equals(userinput));
if (bool)
query = query.Where(i => i.column1.Equals(userinput1));
if (bool)
query = query.Where(i =>i.column2.Equals(userinput2));
var results = query.ToList();
return results;
}
However I want to be able to also filter results where the user input matches a column in one of the child tables.
e.g something like
if (bool)
query = query.Where(i =>i.childtable2.column2.Equals(userinput2));
Is there a way to add it to my current method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,你可以这样做:
In this case you could do something like this: