Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

A full recompile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top