LINQ to SQL和编译查询

时间:2011-02-03 19:59:11

标签: c# .net linq linq-to-sql

我有一个返回IQueryable<tblMyTable>的编译查询。如果我执行编译的查询,结果是否缓存在我传递的DataContext中?

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应用程序。

2 个答案:

答案 0 :(得分:2)

是的,再次执行查询。您可以通过与您的应用程序并行运行SQL事件探查器来查看它。

答案 1 :(得分:0)

使这个工作的唯一方法是首先获取所有记录并执行ToList()或ToArray(),然后对列表或数组运行Count()和Take(10)。但我猜你不想得到所有的结果。

您可以在此处执行的一项优化是明确打开和关闭连接,但我已经读过连接池非常有效,您可能没有注意到太多差异。

This info might clarify things