performancelinq-to-entitieslinq.compiledquery

Does applying additional clauses to a compiled query cause a recompile?


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?


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.