本文介绍了实体框架上的多个 .Where() 子句可查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用实体框架实现一个复杂的过滤器:我想根据我提供的搜索条件将 where
子句添加到可查询对象.
I am trying to implement a complex filter using Entity Framework: I want to add where
clauses to the queryable object based on my provided search criteria.
我可以在 Entity Framework 6 中执行以下操作吗?
Can I do the following in Entity Framework 6?
var queryable = db.Users.Where(x => x.Enabled && !x.Deleted);
// Filter
var userId = User.Identity.GetUserId();
queryable.Where(x => x.AspNetUser.Id == userId);
queryable.Where(x => x.Status >= 2);
// ...etc
我知道我能做到:
var queryable = db.Users
.Where(x => x.Enabled && !x.Deleted)
.Where(x => x.AspNetUser.Id == userId)
.Where(x => x.Status >= 2);
但是我没有通过这个解决方案获得预期的结果.它似乎忽略了秒两个 where
子句.
But I am not getting the expected results with this solution. It seems to be ignoring the seconds two where
clauses.
推荐答案
.Where
返回一个可查询的,它不修改原来的.所以你只需要保存新的可查询对象.
.Where
returns a queryable, it does not modify the original. So you just need to save the new queryable.
var queryable = db.Users.Where(x => x.Enabled && !x.Deleted);
// Filter
var userId = User.Identity.GetUserId();
queryable = queryable.Where(x => x.AspNetUser.Id == userId);
queryable = queryable.Where(x => x.Status >= 2);
// ...etc
这篇关于实体框架上的多个 .Where() 子句可查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!