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

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

王林
Release: 2023-09-03 19:57:08
forward
927 people have browsed it

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>
Copy after login

Sql statement generated for the above query

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

Queryable

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

for the above query Generated Sql statement

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template