是否对已编译的查询应用其他子句会导致重新编译?

时间:2010-07-15 20:09:04

标签: performance linq-to-entities linq.compiledquery

如果我通过CompiledQuery.Compile进行了编译实体查询,然后我又修改了另一个.Where()子句或.OrderBy()子句,这些添加子句是强制完全重新编译,部分重新编译还是不重新编译? / p>

3 个答案:

答案 0 :(得分:2)

所有添加的子句导致不同的查询,因此重新编译。如果您想确保不进行重新编译,请使用.AsEnumerable().ToList()完成对查询的调用。这实现了查询,之后您可以完成所需的所有订购等。

根据您的要求,请参阅this msdn article

答案 1 :(得分:1)

完全重新编译。

答案 2 :(得分:0)

使用已编译的查询

public static Func<DataClasses1DataContext, IQueryable<ErrorLog>>
    GetErrorLogs = CompiledQuery.Compile
    ((DataClasses1DataContext context) =>
        context.ErrorLogs.Where(el => el.UserName != "foo"));

这样叫:

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();
}

输出就像这样

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

唯一的结论是没有重新编译,但.Where(el=>el.ErrorMessage.Contains("foo"))在LINQ2SQL查询产生的对象上应用了LINQ2Object。