0

I am working on an asp.net core 2.0 mvc web application. I have an sql-server database.

I need to build a complex filter. Please note i have simplified the example bellow.

        Func<PersonEntity, bool> expr1 = (x => x.email.Contains("value1");
        Func<PersonEntity, bool> expr2 = (x => x.email.Contains("value2");
        Func< PersonEntity, bool> filter1 = (x => expr1(x) || expr2(x));
        bdd.persons.Where(x => x.id<100).Where(filter1);

The SQL query which is received by sql server is:

SELECT * FROM Persons WHERE id<100

I suppose that my dynamic filter filter1 is executed by C# code. What i want is to execute all filters in sql query. How can i do this ?

Thanks

Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • You should learn how to compose filters from **expressions** (`Expression>`). With `Func<..>` all you get is EF Core [client evaluation](https://learn.microsoft.com/en-us/ef/core/querying/client-eval). Check out [LINQKit](https://github.com/scottksmith95/LINQKit) package - it explains the problem and provides a solution. Or just the `PredicateBuilder` part. There are other predicate builders available as well. – Ivan Stoev Jan 12 '18 at 19:16
  • In fact i want to understand how this package works – Bob5421 Jan 12 '18 at 19:23
  • Then just take a look at `PredicateBuilder` class code. Also my own [PredicateUtils](https://stackoverflow.com/questions/36246162/establish-a-link-between-two-lists-in-linq-to-entities-where-clause/36247259#36247259), or [Universal PredicateBuilder](https://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/) etc. – Ivan Stoev Jan 12 '18 at 19:28

0 Answers0