Dynamically generated SQL contains inline constants, which causes SQL Server to compile the query plan every time a new constant is provided for the same query. Compilation is a costly operation and it can be avoided if the generated SQL is parameterized.
Expression.Constant calls can be replaced by a "wrapped" constant as explained in the article below so that the generated SQL is parameterized and the query plan will be cached by the SQL Server.
https://github.com/graeme-hill/gblog/blob/master/source_content/articles/2014.139_entity-framework-dynamic-queries-and-parameterization.mkd
Dynamically generated SQL contains inline constants, which causes SQL Server to compile the query plan every time a new constant is provided for the same query. Compilation is a costly operation and it can be avoided if the generated SQL is parameterized.
Expression.Constantcalls can be replaced by a "wrapped" constant as explained in the article below so that the generated SQL is parameterized and the query plan will be cached by the SQL Server.https://github.com/graeme-hill/gblog/blob/master/source_content/articles/2014.139_entity-framework-dynamic-queries-and-parameterization.mkd