Search code examples
entity-frameworkentity-framework-core

In EntityFramework does Take(5) have to come after all Where() clauses?


I had a query set up (lots of conditionals in the code deciding what Where() clauses to add.

I found that if I did Take(5) and then the Where() clauses, I got nothing. But if I put it after the Where() clauses, then it works as expected.

Is this by design?


Solution

  • Take(5) should always go at the end of a query. You are essentially saying, once the query has been executed, to take the first 5 results. I'm not sure what happens if Take(x) is executed before Where(), but my guess would be it's just selecting the first x entries in the database and executing the Where() on that, which is not likely to return anything meaningful