Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ到SQL和编译查询_C#_.net_Linq_Linq To Sql - Fatal编程技术网

C# LINQ到SQL和编译查询

C# LINQ到SQL和编译查询,c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,我有一个编译后的查询,返回一个IQueryable。如果执行编译后的查询,结果是否缓存在我传递的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?

我有一个编译后的查询,返回一个
IQueryable
。如果执行编译后的查询,结果是否缓存在我传递的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应用程序。

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

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

这里可以做的一个优化是显式地打开和关闭连接,但我已经读到连接池非常有效,您可能不会注意到太多差异


请不要缩短标签,尤其是我亲爱的LINQ to SQL%)我没有缩短任何标签,我选择的都是自动完成给我的。