简单解释一下:从网上看到IQueryable接口与IEnumberable接口的区别,都是简单的说了一下下面第一点的原因,没有实际的代码,我就简单的在叙述一下其区别的详细的过程。
1:这是从网上找的区别的原因,好多都是这同样的解释:
IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable<T> 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。
2:我设置一个查询,代码如下:
//返回IEnumerable开始
var set3 = (from c in db.cms_certificateDetails select c).OrderByDescending(c => c.id).AsEnumerable()
.Skip(150).Take(10);
foreach (var cmsCertificateDetailse in set3)
{
Console.WriteLine(cmsCertificateDetailse.czry + ";" + cmsCertificateDetailse.kddh + "
");//打印结果
}
用sql server profiler监视的sql如下:
SELECT
[Extent1].[id] AS [id],
[Extent1].[kddh] AS [kddh],
[Extent1].[czry] AS [czry],
[Extent1].[yjnr] AS [yjnr],
[Extent1].[tjTime] AS [tjTime],
[Extent1].[bz] AS [bz],
[Extent1].[forId] AS [forId]
FROM [dbo].[cms_certificateDetails] AS [Extent1]
ORDER BY [Extent1].[id] DESC
//返回IEnumerable结束
//返回IQueryable开始
var set4 = (from c in db.cms_certificateDetails select c).OrderByDescending(c => c.id).AsQueryable()
.Skip(150).Take(10);
foreach (var cmsCertificateDetailse in set4)
{
Console.WriteLine(cmsCertificateDetailse.czry + ";" + cmsCertificateDetailse.kddh + "
");//打印结果
}
用sql server profiler监视的sql如下:
SELECT TOP (10)
[Extent1].[id] AS [id],
[Extent1].[kddh] AS [kddh],
[Extent1].[czry] AS [czry],
[Extent1].[yjnr] AS [yjnr],
[Extent1].[tjTime] AS [tjTime],
[Extent1].[bz] AS [bz],
[Extent1].[forId] AS [forId]
FROM ( SELECT [Extent1].[id] AS [id], [Extent1].[kddh] AS [kddh], [Extent1].[czry] AS [czry], [Extent1].[yjnr] AS [yjnr], [Extent1].[tjTime] AS [tjTime], [Extent1].[bz] AS [bz], [Extent1].[forId] AS [forId], row_number() OVER (ORDER BY [Extent1].[id] DESC) AS [row_number]
FROM [dbo].[cms_certificateDetails] AS [Extent1]
) AS [Extent1]
WHERE [Extent1].[row_number] > 150
ORDER BY [Extent1].[id] DESC
//返回IQueryable结束
3:这下就看出来了,返回IQueryable类型的方法,执行条件查询是放到数据库中的,返回到内存的数据是已经过滤的,然而返回IEnumerable类型的方法,执行条件肯定是从内存中进行过滤的,返回到内存的数据是没有经过过滤的!