If I have a compiled entities query via CompiledQuery.Compile and I then tack on another .Where() clause or .OrderBy() clause, do these addition clauses force a full recompile, a partial recompile, or no recompile?
Asked
Active
Viewed 179 times
3 Answers
2
All added clauses result in a different query, and therefore a recompile. If you want to be sure you are not doing a recompile, finish the call to the query with a .AsEnumerable()
or .ToList()
. This materializes the query, and after that you can do all the ordering etc. you want.
As per your request, see this msdn article.

Glorfindel
- 21,988
- 13
- 81
- 109

Carvellis
- 3,992
- 2
- 34
- 66
1
A full recompile.

Craig Stuntz
- 125,891
- 12
- 252
- 273
-
1I do, but if you don't actually trust the answer you get here, why ask? I can understand a certain degree of skepticism, but the answer is so easy to find that the cynical user might as well just go find it. I answer questions here as an experienced developer, not as a personal librarian. – Craig Stuntz Jul 15 '10 at 21:18
-
Unfortunately with this class of question it is difficult to determine if your answer is correct without sourcing. Personally I spent several hours looking for the answer on Google, but perhaps my Google-fu is much weaker than yours. But if someone else came along and stated another answer, without sourcing, how would I determine the correct one? To me this type of question relies on people who have found the correct answer supplying that answer in the form of a verifiable program or a sourced answer. If that offends you I apologize. – Orion Adrian Jul 16 '10 at 03:48
0
With the compiled query
public static Func<DataClasses1DataContext, IQueryable<ErrorLog>>
GetErrorLogs = CompiledQuery.Compile
((DataClasses1DataContext context) =>
context.ErrorLogs.Where(el => el.UserName != "foo"));
called like this:
using (DataClasses1DataContext context = new DataClasses1DataContext())
{
context.Log = Console.Out;
var res1 = GetErrorLogs(context).ToList();
var res2 = GetErrorLogs(context).Where(el=>el.ErrorMessage.Contains("foo")).ToList();
}
the output is like this
SELECT [t0].[ErrorLogID], [t0].[ErrorTime], [t0].[UserName], [t0].[ErrorNumber], [t0].[ErrorSeverity], [t0].[ErrorState], [t0].[ErrorProcedure], [t0].[ErrorLine], [t0].[ErrorMessage]
FROM [dbo].[ErrorLog] AS [t0]
WHERE [t0].[UserName] <> @p0
-- @p0: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [foo]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
SELECT [t0].[ErrorLogID], [t0].[ErrorTime], [t0].[UserName], [t0].[ErrorNumber], [t0].[ErrorSeverity], [t0].[ErrorState], [t0].[ErrorProcedure], [t0].[ErrorLine], [t0].[ErrorMessage]
FROM [dbo].[ErrorLog] AS [t0]
WHERE [t0].[UserName] <> @p0
-- @p0: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [foo]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
The only conclusion is that there is no recompile, but the .Where(el=>el.ErrorMessage.Contains("foo"))
is applied with LINQ2Objects on the objects resulting from the LINQ2SQL query.

Albin Sunnanbo
- 46,430
- 8
- 69
- 108
-
I don't know how you did your testing, but adding a where after a compiled query definitely does result in a recompile. – Carvellis Apr 02 '11 at 08:43