2011-02-03 66 views
2

我有一個編譯後的查詢返回IQueryable<tblMyTable>。如果我執行已編譯的查詢,那麼在我傳遞的DataContext中緩存的結果是什麼?LINQ to SQL和已編譯的查詢

using(context) 
{ 
    var count = MyCompiledQuery(context).Count(); 

    //Does the call to MyCompiledQuery execute against the database again, or does it go to the context for results? 
    var first10 = MyCompiledQuery(context).Take(10); 
} 

這是使用C#的.NET一個3.5應用程序。

+1

請不要縮短標籤,尤其是我心愛的LINQ to SQL%) – abatishchev 2011-02-03 20:28:18

回答

2

是的,查詢再次執行。您可以通過與您的應用程序並行運行SQL Profiler來查看它。

0

你得到這個工作的唯一方法是首先獲取所有的記錄,然後執行ToList()或ToArray(),然後Count()和Take(10)將對列表或數組運行。但我猜你不想得到所有的結果。

你可以在這裏做的一個優化是明確地打開和關閉連接,但我讀過連接池非常高效,以至於你可能沒有注意到太多的區別。

This info might clarify things