您的位置:首页 > 编程学习 > ASP.NET > 正文

IQueryable与IEnumberable的区别

更多 时间:2014-12-16 类别:编程学习 浏览量:418

IQueryable与IEnumberable的区别

IQueryable与IEnumberable的区别

一、IEnumerable接口

公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。也就是说:实现了此接口的object,就可以直接使用foreach遍历此object

 

二、IQueryable 接口

它继承 IEnumerable 接口,而因为.net版本加入Linq和IQueryable后,使得IEnumerable不再那么单调,变得更加强大和丰富。

 

三、IQueryable与IEnumberable的区别

1、IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了

2、IQueryable<T> 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。

 

四、IQueryable与IEnumberable的实例说明一

1、IEnumerable实例

 

  • C# 代码   复制
  • 
    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");//打印结果
    }
    
    		
  • 用sql server profiler监视的sql如下

     

  • 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
    
    		
  • 2、IQueryable实例

     

  • C# 代码   复制
  • 
    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如下

     

  • 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与IEnumberable的实例说明二

     

  •  
  • C# 代码   复制
  • 
    static void Main(string[] args)
            {
                //创建数据库访问网关
                using (SchoolDBEntities schoolEntities = new SchoolDBEntities())
                {
                    //查询的结果放入IQueryable接口的集合中
                    IQueryable<T_Class> classesIQue = (from c in schoolEntities.T_Class
                                                       orderby c.ID
                                                         select c).Skip<T_Class>(3).Take<T_Class>(3);
                    //注意这个AsEnumerable<T_Class>()在分页查询之前,先将其转换成IEnumerable类型
                    IEnumerable<T_Class> classesIEnu = (from c in schoolEntities.T_Class
                                                        orderby c.ID   
                                                        select c).AsEnumerable<T_Class>().Skip<T_Class>(3).Take<T_Class>(3);
                    //因为启用了延迟加载机制,所以下面调用一下,才会真正去读取数据库
                    int i = 0;
                    foreach (var c in classesIQue)
                    {
                        i++;
                    }
                    Console.WriteLine(i);
                    foreach (var c in classesIEnu)
                    {
                        i++;
                    }
                    Console.WriteLine(i);
                }
                Console.WriteLine("OK");
                Console.ReadKey();
            }
    
    		
  • 最终执行的sql

    1、直接返回 IQueryable类型的查询,如下图所示:

     

  •  
  •  
  • 2、分页查询之前先将其转换成 IEnumerable实际执行的sql如下图所示
  •  
  •  
  •  
  •  
  • 标签:List
  • 上一篇:mongodb用户权限
  • 下一篇:plsql常用函数
  • 您可能感兴趣