c# – 在Linq到实体中组合谓词

我想动态构建我的条件列表.这是我的代码片段:

protected Expression<Func<event_info, bool>> _wherePredicate = c => true;

public void main() 
{

 _wherePredicate = _wherePredicate.And(c => c.createdby == 6);
 _wherePredicate = _wherePredicate.And(c => c.isdeleted == 0);

 var query = from ev in dataConnection.event_info
                       where ev.isdeleted == 0
                       select ev;
 Results = query.Where(_wherePredicate).ToList(); 
}

除非这不起作用,因为linq-to-entities不支持Invoke方法.

我可以在linq-to-entities中组合谓词的好方法是什么?

最佳答案 事实证明,你需要添加这个:

结果= query.AsExpandable.Where(_wherePredicate).ToList();

然后它就神奇地起作用了!

我按照本教程:
http://www.albahari.com/nutshell/predicatebuilder.aspx

点赞