- 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!