您如何在实体框架查询中将Expression<Func<T, bool>>
与bool
进行比较?
这是我想要做的一个例子:
Expression<Func<Errand, bool>> isClosed = (x)
=> x.ClosedDate.HasValue;
bool input = false;
var model = db.Errands
.Where(isClosed == input) // comparing to just boolean is not valid
.ToList();
你怎么能实现这个目标?
答案 0 :(得分:2)
一种选择是使用方法动态创建表达式:
public Expression<Func<Errand, bool>> IsClosed(bool closed)
{
return x => x.ClosedDate.HasValue == closed;
}
并像这样使用它:
bool input = false;
var model = db.Errands
.Where(IsClosed(input))
.ToList();