IQueryable 和 IEnumerable

楼主最近看了下

IQueryable 和 IEnumerable

的区别。

当真被忽悠的死去活来。。。

网上都说 IQueryable 和 IEnumerable区别很大,然后怎么着怎么着。。。可怜


然后我就去测试了啊

先拿出我的数据库表结构。


这里是我的查询语句。比较简单

TestDataEntities db = new TestDataEntities();
            IQueryable<Orders> q = db.Orders.OrderBy(x=>x.id).Skip(1).Take(2);
            IEnumerable<Orders> e = db.Orders.OrderBy(x=>x.id).Skip(1).Take(2);

            Console.WriteLine("IQueryable : \r\n"+q.ToString());
            Console.WriteLine("---------------------------------------------------------");
            Console.WriteLine("IEnumerable : \r\n" + e.ToString());

输出结果:

IQueryable :

SELECT TOP (2) 
[Extent1].[id] AS [id], 
[Extent1].[parentid] AS [parentid], 
[Extent1].[code] AS [code]
FROM ( SELECT [Extent1].[id] AS [id], [Extent1].[parentid] AS [parentid], [Extent1].[code] AS [code], row_number() OVER (ORDER BY [Extent1].[id] ASC) AS [row_number]
	FROM [dbo].[Orders] AS [Extent1]
)  AS [Extent1]
WHERE [Extent1].[row_number] > 1
ORDER BY [Extent1].[id] ASC


IEnumerable :

SELECT TOP (2) 
[Extent1].[id] AS [id], 
[Extent1].[parentid] AS [parentid], 
[Extent1].[code] AS [code]
FROM ( SELECT [Extent1].[id] AS [id], [Extent1].[parentid] AS [parentid], [Extent1].[code] AS [code], row_number() OVER (ORDER BY [Extent1].[id] ASC) AS [row_number]
	FROM [dbo].[Orders] AS [Extent1]
)  AS [Extent1]
WHERE [Extent1].[row_number] > 1
ORDER BY [Extent1].[id] ASC


有区别吗?没有哇!!!


后来楼主就较真了!!!!!!是的,较真了!!!为何网上这么多人说有区别!发火发火发火发火

然后发现。。。。是这个原因!~

看楼下代码

TestDataEntities db = new TestDataEntities();
            IQueryable<Orders> q = db.Orders.OrderBy(x => x.id).AsQueryable<Orders>().Skip(10).Take(2);
            IEnumerable<Orders> e = db.Orders.OrderBy(x => x.id).AsEnumerable<Orders>().Skip(10).Take(2);

            foreach (var item in q)
            {
                Console.WriteLine("123");
            }

            foreach (var item in e)
            {
                Console.WriteLine("234");
            }

原因其实出在

AsQueryable 和 AsEnumerable  

反正就是加载方式不同

上面代码的sql语句是这样的!

IQueryable :

SELECT TOP (2) 
[Extent1].[id] AS [id], 
[Extent1].[parentid] AS [parentid], 
[Extent1].[code] AS [code]
FROM ( SELECT [Extent1].[id] AS [id], [Extent1].[parentid] AS [parentid], [Extent1].[code] AS [code], row_number() OVER (ORDER BY [Extent1].[id] ASC) AS [row_number]
FROM [dbo].[Orders] AS [Extent1]
)  AS [Extent1]
WHERE [Extent1].[row_number] > 10
ORDER BY [Extent1].[id] ASC


IEnumerable :

SELECT 
[Extent1].[id] AS [id], 
[Extent1].[parentid] AS [parentid], 
[Extent1].[code] AS [code]
FROM [dbo].[Orders] AS [Extent1]
ORDER BY [Extent1].[id] ASC

是的,你没看错,奇怪在于IEnumerable生成的语句,是查询全表。

聪明的你肯定会联想到。IEnumerable是通过缓存全表,然后才分页查找的!!!所以IEnumerable性能不好!

为什么????!!!!

因为:

IQueryable     是 linq to sql           这家伙操作数据库

IEnumerable 是  linq to object     这家伙操作内存

区别在这里!



当然网上还有说IQueryable 和 IEnumerable其他区别(这个楼主没有验证)

IEnumerable<T>查询必须在本地执行.并且执行查询前我们必须把所有的数据加载到本地


总结一下:

IQueryable     是 linq to sql         这家伙操作数据库

IEnumerable 是  linq to object     这家伙操作内存

IEnumerable 会查询全部数据,然后在内存里进行分页或者筛选操作。

但是真正导致他们使用哪种方式的,是AsQueryable() 和 AsEnumerable()  !!!!!






posted @ 2015-11-27 14:42  正怒月神  阅读(111)  评论(0编辑  收藏  举报