[英]Comparing Expression<Func<T, bool>> to bool in query
How would you compare Expression<Func<T, bool>>
to bool
in an Entity Framework query?在实体框架查询中
Expression<Func<T, bool>>
您如何比较Expression<Func<T, bool>>
与bool
?
Here is an example of what I am trying to do:这是我正在尝试做的一个例子:
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();
How could you achieve this?你怎么能做到这一点?
One option is to create the expression dynamically with a method:一种选择是使用方法动态创建表达式:
public Expression<Func<Errand, bool>> IsClosed(bool closed)
{
return x => x.ClosedDate.HasValue == closed;
}
And use it like this:并像这样使用它:
bool input = false;
var model = db.Errands
.Where(IsClosed(input))
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.