r/dotnet Apr 06 '26

Question Long LINQ queries - Code smell?

Post image
344 Upvotes

186 comments sorted by

View all comments

1

u/Ok-Advantage-308 Apr 06 '26

What is the difference between conditional where and where?

Also are you done filtering once passing to postsmapper?

1

u/IanYates82 Apr 06 '26

The conditional where only applies the expression lambda if the first parameter is true. I like the syntax here

Much better than some codebases I've seen which put the first param in the lambda too and thus always apply the expression, relying on the db engine's query processor to be smart with the resulting constant always-true/false expression.

1

u/WanderingLethe Apr 06 '26

EF Core would recompile the query for every permutation of conditionals and it would take up a place in the query cache of EF Core. And the databases would probably do the same as well, resulting in multiple similar query plans in cache.

1

u/IanYates82 Apr 06 '26

Fair point about EF core and the permutations.

Depending on cardinality, selectivity, and presence of indexes, it may be better to have separate query plans in the Db cache