8

LINQ to SQL always converts values into query parameters in the output. For example:

someTable.Where(n => n.Field == 5);

produces:

WHERE Field = @p0

This causes problems on certain query optimization scenarios. Is there a way to force values to be inlined in generated SQL, so that it becomes:

WHERE Field = 5

? Would LINQ to Entities provide a way or does it behave the same?

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
  • What are those "scenarios" ? – ErikEJ Dec 01 '14 at 11:37
  • 1
    @ErikEJ in one of my queries generated by LINQ to SQL, using parameter is 10x slower than using inline values. that makes sense due to how query optimizers work. i'm not looking for optimizing my query itself but trying to address the broader problem of "unnecessary parameterization" which serves no purpose at all. – Sedat Kapanoglu Dec 01 '14 at 13:00
  • 1
    [This question](http://stackoverflow.com/q/510214/861716) gives some background info. – Gert Arnold Dec 02 '14 at 21:56
  • 1
    I know this is a 2+ years old question... but asked the same thing. https://stackoverflow.com/questions/44709877/entity-framework-6-parameter-query-11x-slower-than-inline-parameters-query-wh – TravisWhidden Jun 22 '17 at 21:54

1 Answers1

2

As Diego Vega from Entity Framework team mentioned on Twitter:

constants have actually translated as inline constants in EF forever.

so it seems like EF is the way to go, with one catch:

We believe EF7 should parameterize more so we need to hear when that is a bad for you

at least EF6 is much better than LINQ to SQL in that manner so we can use EF instead.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148