簡單解釋一下:從網上看到IQueryable接口與IEnumberable接口的區別,都是簡單的說了一下下面第一點的緣由,沒有實際的代碼,我就簡單的在敘述一下其區別的詳細的過程。 sql
1:這是從網上找的區別的緣由,好多都是這一樣的解釋:數據庫
IEnumerable<T> 泛型類在調用本身的SKip 和 Take 等擴展方法以前數據就已經加載在本地內存裏了,而IQueryable<T> 是將Skip ,take 這些方法表達式翻譯成T-SQL語句以後再向SQL服務器發送命令,它並非把全部數據都加載到內存裏來才進行條件過濾。服務器
2:我設置一個查詢,代碼以下:spa
//返回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 + "\r\n");//打印結果
}server
用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] DESCip
//返回IEnumerable結束內存
//返回IQueryable開始it
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 + "\r\n");//打印結果
}
用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類型的方法,執行條件確定是從內存中進行過濾的,返回到內存的數據是沒有通過過濾的!