本文章是从大牛那边摘录过来,欢迎大家参考其区别和用途。欢迎指正。
IEnumerable接口
公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。也就是说:实现了此接口的object,就可以直接使用foreach遍历此object;
IQueryable 接口
它继承 IEnumerable 接口,而因为.net版本加入Linq和IQueryable后,使得IEnumerable不再那么单调,变得更加强大和丰富。
为了区别两个接口,我们通过一个实际的例子来解释一下。
#region 用户管理列表
/// <summary>
/// 用户管理列表
/// </summary>
/// <returns></returns>
public ActionResult List()
{
//IQueryable
IQueryable<m_user> userlistIQue = (from u in myteref.m_user
orderby u.id
select u).Skip<m_user>(1).Take<m_user>(2);
//IEnumerable
IEnumerable<m_user> userlistIEnu = (from u in myteref.m_user
orderby u.id
select u).AsEnumerable<m_user>().Skip<m_user>(1).Take<m_user>(2);
int i = 0;
foreach (var c in userlistIQue)
{
i++;
}
Console.WriteLine(i);
foreach (var c in userlistIEnu)
{
i++;
}
Console.WriteLine(i);
return View();
}
#endregion
IQueryable的,会先帮你执行Skip<m_user>(1).Take<m_user>(2),生成SQL:SELECT TOP 2 ....
IEnumerable ,则是查询出所有的数据,放到内存中来。
.AsEnumerable() 和 .AsQueryable()
string strcon = "Data Source=.\\SQLEXPRESS;Initial Catalog=Db_Example;Persist Security Info=True;User ID=sa;Password=sa";
SqlConnection con = new SqlConnection(strcon);
con.Open();
string strsql = "select * from SC,Course where SC.Cno=Course.Cno";
SqlDataAdapter da = new SqlDataAdapter(strsql,con);
DataSet ds = new DataSet();
da.Fill(ds, "mytable");
DataTable tables=ds.Tables["mytable"]; //创建表
var dslp = from d in tables.AsEnumerable() select d;//执行LINQ语句,这里的.AsEnumerable()是延迟发生,不会立即执行,实际上什么都没有发生
foreach(var res in dslp)
{
Response.Write(res.Field<string>("Cname").ToString());
}
1、AsEnumerable()是延迟执行的,实际上什么都没有发生,当真正使用对象的时候(例如调用:First, Single, ToList....的时候)才执行。
2、AsEnumerable将一个序列向上转换为一个IEnumerable, 强制将Enumerable类下面的查询操作符绑定到后续的子查询当中。
3、AsQueryable将一个序列向下转换为一个IQueryable, 它生成了一个本地查询的IQueryable包装。
4、AsEnumerable()延迟执行,不会立即执行。当你调用.AsEnumerable()的时候,实际上什么都没有发生。
5、ToList()立即执行
6、当你需要操作结果的时候,用.ToList(),否则,如果仅仅是用来查询不需要进一步使用结果集,并可以延迟执行,就用.AsEnumerable()/IEnumerable /IQueryable
7、AsEnumerable()虽然延迟执行,但还是访问数据库,而.ToList()直接取得结果放在内存中。比如我们需要显示两个部门的员工时,部门可以先取出放置在List中,然后再依次取出各个部门的员工,这时访问的效率要高一些,因为不需要每次都访问数据库去取出部门。
8、IQueryable实现了IEnumberable接口。但IEnumerable<T> 换成IQueryable<T>后速度提高很多。
9、IQueryable接口与IEnumberable接口的区别: IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable<T> 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。
10、IEnumerable跑的是Linq to Object,强制从数据库中读取所有数据到内存先。
二、AsEnumerable和AsQueryable的实例
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]
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]
二、AsEnumerable和AsQueryable的实例
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]
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]