Home  >  Article  >  Backend Development  >  What is the difference between IEnumerable and IQueryable in C#?

What is the difference between IEnumerable and IQueryable in C#?

王林
王林forward
2023-09-03 19:57:08834browse

C# 中 IEnumerable 和 IQueryable 有什么区别?

  • IEnumerable exists in the System.Collections namespace.
  • IQueryable exists in System. Linq namespace.
  • IEnumerable and IQueryable are both forward collections.
  • IEnumerable does not support lazy loading
  • IQueryable supports lazy loading
  • When querying data from the database, IEnumerable performs a select query on the server side and loads the data into memory on the client side , and then filter the data.
  • To query data from the database, IQueryable performs a select query on the server side with all filters.
  • IEnumerable extension methods take function objects.
  • The IQueryable extension method takes an expression object meaning an expression tree.

Example

IEnumerable

dbContext dc = new dbContext ();
IEnumerable
<SocialMedia>
list = dc.SocialMedias.Where(p => p.Name.StartsWith("T"));
list = list.Take<SocialMedia>(1);
</SocialMedia>

Sql statement generated for the above query

SELECT [t0].[ID], [t0].[Name] FROM [SocialMedia] AS [t0]
WHERE [t0].[Name] LIKE @p0

Queryable

dbContext dc = new dbContext ();
IQueryable<SocialMedia> list = dc.SocialMedias.Where(p => p.Name.StartsWith("T"));
list = list.Take<SocialMedia>(1);

for the above query Generated Sql statement

SELECT top 1 [t0].[ID], [t0].[Name] FROM [SocialMedia] AS [t0]
WHERE [t0].[Name] LIKE @p0

The above is the detailed content of What is the difference between IEnumerable and IQueryable in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete