簡體   English   中英

將其他子句應用於已編譯查詢是否會導致重新編譯?

[英]Does applying additional clauses to a compiled query cause a recompile?

如果我有一個通過CompiledQuery.Compile進行的已編譯實體查詢,然后又添加了另一個.Where()子句或.OrderBy()子句,這些附加子句是否會強制進行完全重新編譯,部分重新編譯或不重新編譯?

所有添加的子句都會導致不同的查詢,因此需要重新編譯。 如果要確保沒有進行重新編譯,請使用.AsEnumerable().ToList()完成對查詢的調用。 這樣就實現了查詢,然后您可以進行所需的所有排序等。

根據您的請求,請參閱此msdn文章

完全重新編譯。

與編譯查詢

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"))與LINQ2Objects一起應用於LINQ2SQL查詢產生的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM