Combine Predicates in Linq-to-entities

时光总嘲笑我的痴心妄想 提交于 2019-12-19 19:49:04

问题


I want to dynamically build my list of where conditions. Here's a snippet of my code:

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(); 
}

Except this doesn't work because linq-to-entities doesn't support the Invoke method.

What's a good way I can combine predicates in linq-to-entities?


回答1:


Turns out, you need to add this:

Results = query.AsExpandable.Where(_wherePredicate).ToList();

And then it just magically works!

I followed this tutorial: http://www.albahari.com/nutshell/predicatebuilder.aspx



来源:https://stackoverflow.com/questions/10875547/combine-predicates-in-linq-to-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!