Ads

C# Programming 12 - Which one is better IEnumerable or IQueryable?

 Hello Friends,

Good Morning, Afternoon, Evening what ever zone you are in. Myself Chakrapani Upadhyaya a Full Stack Engineer.

In this article we will look into the usage of IEnumerable and .IQueryable.


Great! both will give you deferred execution, But the main difference between is about where the filter logic is executed.

IEnumerable ==> LINQ-to-object

IQueryable    ==>  LINQ-to-SQL

Lets assume we have ProductInfo table in MS SQL Server, and we are retrieving the data from that using Entity Framework




When we use IEnumerable.

IEnumerable<ProductInfo> first = dbContext.POroductInfo
                                 .Where(c => c.StoreName > "Laptop")

In SQL Server profiler we find a command equal to:

SELECT * FROM [dbo].[ProductInfo]

When we use IQueryable.

In SQL Server profiler we find a command equal to:

SELECT * FROM [dbo].[ProductInfo] WHERE [StoreName]='Laptop'

So its clear now, when we use IEnumerable So, all table records are loaded into memory as objects, and then with each .Where() it will be another filter in memory against these objects.

But in IQueryable, the compiler passes an expression tree to Where(). at the end this expression will be converted to an SQL query to run on the database engine. 

I hope you enjoyed this article. 

That's it for right now, see you in next article until then take care bye bye 😄

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !