我正在尝试使用Entity Framework实现复杂的过滤器:我想where
根据我提供的搜索条件向可查询对象添加子句.
我可以在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
条款.
.Where
返回一个可查询的,它不会修改原始的.所以你只需要保存新的可查询.
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