Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine Predicates in Linq-to-entities

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?

like image 387
John Zumbrum Avatar asked Oct 07 '22 15:10

John Zumbrum


1 Answers

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

like image 128
John Zumbrum Avatar answered Oct 10 '22 07:10

John Zumbrum