-2

I have some dynamic conditions that I want to use in my Select clause. So I would like to create a "base query" like "SELECT * FROM TABLE" and after I wuold like to add conditions like "WHERE name = 'Diego' AND ...". I'm using Entity Core, and the solution I found (it's working) is as follows:

//conditions
Expression<Func<Person, bool>> foo = (p => p.Id == 1);
//select
List<Person> people = Db.People.Where(foo).ToList();
//Db is the context, People is the DbSet and Person is the model

I hope there is a simple or standard way to do that, in fact I have tried some, but they haven't worked.

Anyway where can I find a good tutorial about it, from simple to complex?

---------------------- UPDATE -----------------------------

Well, this question has marked as duplicate, but this Multiple .Where() clauses on an Entity Framework Queryable , doesn't worked for me,I don't know why, but the statement executes before the conditions, (if I do the same thing like the answer on the link), and I want to apply the conditions before the first statement execution. Yes I know I must use toList() just at the end, anyway if I do the same thing like the answer on that link, the statement will execute before, and I agree this is very strange.

---------------------- UPDATE -----------------------------

I provide some kind of proof for what I'm talking about in the images below:

Here we have a single statement execution (the result is a single Person object), in this example I'm using the code I've written above.

enter image description here

Here as we can see, the code: queryable = Db.People.Where(p => p.Name.Contains("El"));, is executed (the result is two Person objects), even without the method ToList(), in this example I'm using the suggested code.

enter image description here

Well maybe it's a entity core bug. Anyway this question is not the same of that which was been linked for many reasons.

Andre
  • 652
  • 2
  • 7
  • 23
  • Does your code work? That that is a correct way to do it! I wouldn't bother with the `foo` variable though, just put that straight inside the `Where` clause: `.Where(p => p.Id == 1)`. Other than that, I'm either not sure what you're talking about or this question is opinion based. – DavidG Jul 18 '18 at 17:20
  • 1
    @DavidG I think Diego wants to do something like this: https://stackoverflow.com/questions/6353350/multiple-where-conditions-in-ef – Rui Jarimba Jul 18 '18 at 17:22
  • @RuiJarimba Yes, you might be right, but unless you're absolutely sure that's what he is asking for, I'm going to reserve judgement. – DavidG Jul 18 '18 at 17:24
  • @RuiJarimba you are correct, I want to be able to add more conditions dynamically – Andre Jul 18 '18 at 17:30
  • @DavidG yes the code works. – Andre Jul 18 '18 at 17:31
  • @diegocolli you have all the information you need in the above links. Remember to invoke `.ToList()` only after you add **ALL** the `WHERE` clauses!!! – Rui Jarimba Jul 18 '18 at 17:31
  • 1
    Show us your attempt at the duplicate answer. – Jasen Jul 18 '18 at 17:48
  • @Jasen I'll leaving now, but I'll do that, tomorrow, I'll post the example – Andre Jul 18 '18 at 17:50
  • Check LinqPad.net. It has the wonderful LinqPad tool plus PredicateBuilder. There is also https://github.com/kahanu/System.Linq.Dynamic/wiki/Dynamic-Expressions – Cetin Basoz Jul 18 '18 at 17:50
  • @Jasen I've post the example – Andre Jul 18 '18 at 21:04
  • _Expanding the Results View will enumerate the IEnumerable_ the debugger will materialize the query. Is there a problem with the final `.ToList()`? – Jasen Jul 18 '18 at 21:09
  • No, the problem is just: When I try to use the suggested solution, the code executes more than is needed, the final result can be the same, but is less efficient. It will get more data than needed and then throw away what it does not needed – Andre Jul 18 '18 at 21:14

1 Answers1

0

Here as we can see, the code: queryable = Db.People.Where(p => p.Name.Contains("El"));, is executed (the result is two Person objects), even without the method ToList(), in this example I'm using the suggested code.

Sorry but what you are seeing is the fact that you accepted allowing the expression to run. It did NOT run until you opened the results as stated in your own screenshot:

enter image description here

So this is a duplicate, you are just unfamiliar with how the locals/watch windows work.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Oh... I'm really sorry, I didnt know that.. What can I do now? Can I close this question? – Andre Jul 18 '18 at 21:21