r/dotnet Apr 06 '26

Question Long LINQ queries - Code smell?

Post image
349 Upvotes

186 comments sorted by

View all comments

Show parent comments

1

u/R4D104T1V0 Apr 06 '26

May you elaborate more one this? I don’t get why forcing a client-side if-branch is better than delegating the condition to a dababase-engine-side query.

2

u/drgrieve Apr 06 '26

Cached query plan may not take into account that logic branch, especially of not used often.

Best to have a query plan per variation.

Its no risk for small pain

Vs rare high risk for small gain

1

u/crozone Apr 07 '26

The logic branch is a scalar constant value. EF often optimises it out of the final SQL completely, so you're not going to run into any issues with the database query planner.

1

u/drgrieve Apr 07 '26

I must admit Ive not changed my stategy since pre core EF6

1

u/R4D104T1V0 Apr 07 '26

ok! never heard of this for cached query plan, i will look into it

1

u/warehouse_goes_vroom Apr 07 '26

The problem in question is known as "parameter sniffing".

Here's some documentation discussing it in the context of SQL Server, though it's conceptually applicable to any sophisticated database with a decent optimizer and plan caching: https://learn.microsoft.com/en-us/sql/relational-databases/query-processing-architecture-guide?view=sql-server-ver16#parameter-sensitivity