欢迎光临

木子屋

标题: 比较LINQ to Entities的AsQueryable和AsEnumerable方法

作者: dnawo

日期: 2012-12-22 05:24 PM

分类: Web编程

内容: 例一：
C#程序：
复制内容到剪贴板 程序代码using (testContext context = new testContext())
{
var query = (from item in context.Users.AsQueryable()
where item.id > 10
select item.id).ToList();
var query2 = (from item in context.Users.AsEnumerable()
where item.id > 10
select item.id).ToList();
}
服务器端sql：
复制内容到剪贴板 程序代码--AsQueryable
Select
[Extent1].[id] AS [id]
FROM [dbo].[User] AS [Extent1]
Where [Extent1].[id] > 10
--AsEnumerable
Select
[Extent1].[id] AS [id],
[Extent1].[usn] AS [usn],
[Extent1].[pwd] AS [pwd],
[Extent1].[created] AS [created]
FROM [dbo].[User] AS [Extent1]
例二：
C#程序：
复制内容到剪贴板 程序代码using (testContext context = new testContext())
{
var query = (from item in context.Users.AsQueryable()
where item.id > 10
orderby item.id ascending
select item.id).Skip(20).Take(20).ToList();
var query2 = (from item in context.Users.AsEnumerable()
where item.id > 10
orderby item.id ascending
select item.id).Skip(20).Take(20).ToList();
}
服务器端sql：
复制内容到剪贴板 程序代码--AsQueryable
Select TOP (20)
[Filter1].[id] AS [id]
FROM ( Select [Extent1].[id] AS [id], row_number() OVER (ORDER BY [Extent1].[id] ASC) AS [row_number]
FROM [dbo].[User] AS [Extent1]
Where [Extent1].[id] > 10
) AS [Filter1]
Where [Filter1].[row_number] > 20
orDER BY [Filter1].[id] ASC
--AsEnumerable
Select
[Extent1].[id] AS [id],
[Extent1].[usn] AS [usn],
[Extent1].[pwd] AS [pwd],
[Extent1].[created] AS [created]
FROM [dbo].[User] AS [Extent1]
小结
AsQueryable是在数据库中查询再返回数据，AsEnumerable是从数据库读取全部数据再在程序中查询，其效果和ToList相同。

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.703 ms

返回

标题: 比较LINQ to Entities的AsQueryable和AsEnumerable方法

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.703 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.719 ms

返回