views:

39

answers:

3

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?

+1  A: 

A full recompile.

Craig Stuntz
Do you have a source?
Orion Adrian
I 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
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
+1  A: 

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.

Jappie
A: 

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