当前位置:网站首页>The difference between iqueryable and IEnumerable

The difference between iqueryable and IEnumerable

2022-06-22 05:08:00 I left my life to work

IEnumerable Interface

Expose enumerator , The enumerator supports simple iterations over a collection of a specified type . in other words : Implemented this interface object, You can use it directly foreach Traverse this object;

IQueryable Interface

It inherited IEnumerable Interface , And because .net Version to join Linq and IQueryable after , bring IEnumerable No longer so monotonous , Become stronger and richer .

To distinguish the two interfaces , Let's use a practical example to explain .

[TestMethod]
       public void LinqTest()
       {
    
           var db = new SecurityRepositoryContext(new HSLogger());
           var ur = new UserRepository(db);
           // The results of the query are put into IQueryable In the collection of interfaces 
           IQueryable<User> querys = (from c in db.Context.Set<User>()
                                      orderby c.Code
                                      select c).Skip<User>(2).Take<User>(2);
           // Pay attention to this AsEnumerable<User>() Before paging queries , First convert it into IEnumerable type 
           IEnumerable<User> enumers = (from c in db.Context.Set<User>()
                                        orderby c.Code
                                        select c).AsEnumerable<User>().Skip<User>(2).Take<User>(2);
           // Because the delayed loading mechanism is enabled , So let's call , To actually read the database 
           foreach (var c in querys)
           {
    
               //TODO
           }
           foreach (var c in enumers)
           {
     
               //TODO
           }
       }

Trace the code , Take a look at the generated by these two paragraphs when they are executed SQL Code :
The first paragraph , return IQueryable Interface type :

SELECT
    [Extent1].[Id] AS [Id], 
    [Extent1].[Code] AS [Code], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Status] AS [Status], 
    [Extent1].[IsOnline] AS [IsOnline], 
    [Extent1].[SystemVersion] AS [SystemVersion], 
    [Extent1].[CreatedBy] AS [CreatedBy], 
    [Extent1].[CreatedOn] AS [CreatedOn], 
    [Extent1].[ModifiedBy] AS [ModifiedBy], 
    [Extent1].[ModifiedOn] AS [ModifiedOn], 
    [Extent1].[VersionNumber] AS [VersionNumber]
    FROM [dbo].[User] AS [Extent1]
    ORDER BY [Extent1].[Code] ASC
    OFFSET 2 ROWS FETCH NEXT 2 ROWS ONLY

The second paragraph , return IEnumerable Interface type :

SELECT
    [Extent1].[Id] AS [Id], 
    [Extent1].[Code] AS [Code], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Status] AS [Status], 
    [Extent1].[IsOnline] AS [IsOnline], 
    [Extent1].[SystemVersion] AS [SystemVersion], 
    [Extent1].[CreatedBy] AS [CreatedBy], 
    [Extent1].[CreatedOn] AS [CreatedOn], 
    [Extent1].[ModifiedBy] AS [ModifiedBy], 
    [Extent1].[ModifiedOn] AS [ModifiedOn], 
    [Extent1].[VersionNumber] AS [VersionNumber]
    FROM [dbo].[User] AS [Extent1]
    ORDER BY [Extent1].[Code] ASC

Through the above two examples, I have summarized ,IQueryable Interface and IEnumberable Interface differences :

IEnumerable Generic classes are calling their own SKip and Take Before the extension method, the data has been loaded into the local memory , and IQueryable Yes, it will Skip ,take These method expressions are translated into T-SQL Statement and then to SQL The server sends commands , It does not load all data into memory to perform conditional filtering .

原网站

版权声明
本文为[I left my life to work]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220503134473.html