What is the difference between ienumerable and iqueryable




















It is time to update the implementation of EmployeeEnumerable. Now, that we have completed the implementation, we can run the application to check out the implementation.

In the Main method, I will create a new instance of EmployeeEnumerable. Add a couple of employees to the EmployeeEnumerable instance. And after that I will loop through the EmployeeEnumerable instance and print out the employee id. Now that we went through the implementation of IEnumerable , it is clear on how the internal enumeration work. The next logical step now, is to understand how IQueryable works. The IQueryable interface derives from the IEnumerable interface.

And the purpose of IQueryable is more specific compare to the IEnumerable interface. The main purpose of the IQueryable interface is to provide the functionality to query data against a specific data source. The intent of IQueryable is mainly to provide data providers with standard contracts to write query implementation against their data source. From the above definition, it is clear that data providers like Entity Framework Core implements IQueryable.

To show how it works, I am going to use Entity Framework Core to connect to an existing database. You can check out my blog post on a deep dive into Entity Framework Core here. The column names of the Employees table matches with the properties of the Employee class. The only difference between the Employees table and the Employee class is the name. Firstly, to access the employee data from the Employees table, we will need to create a class deriving from DbContext. Hence I will create a new class and name it EmployeeContext.

Thirdly, I will create a constructor for the EmployeeContext class and accept the connection string to the database as a parameter. Fifthly, I will create a local variable for ILoggerFactory instance. I will use the static method Create of LoggerFactory class to do that. And inside of this overridden method, I will take two steps:.

Now, I will update the Main method, to create an instance of EmployeeContext. And once the EmployeeContext instance creation is complete, I will call the Employees property on the EmployeeContext instance. Another important note, I will declare the employees variable as IEnumerable instead of default IQueryable.

This is to initially demonstrate how IEnumerable will behave. As you can see from the above response, there is no change in the actual SQL query.

It is still getting all employees where employee id is greater than 1 in memory. And post that in-memory the reduction is happening. This is a nice video on youtube which demonstrates how these interfaces differ , worth a watch. There are many differences but let us discuss about the one big difference which makes the biggest difference. IEnumerable interface is useful when your collection is loaded using LINQ or Entity framework and you want to apply filter on the collection.

Consider the below simple code which uses IEnumerable with entity framework. This where filter is executed on the client side where the IEnumerable code is. In other words all the data is fetched from the database and then at the client its scans and gets the record with EmpId is 2.

But now see the below code we have changed IEnumerable to IQueryable. It creates a SQL Query at the server side and only necessary data is sent to the client side. So the difference between IQueryable and IEnumerable is about where the filter logic is executed. One executes on the client side and the other executes on the database. IEnumerable: IEnumerable is best suitable for working with in-memory collection or local queries.

IQueryable: IQueryable best suits for remote data source, like a database or web service or remote queries. IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios like paging and composition based queries. So when you have to simply iterate through the in-memory collection, use IEnumerable, if you need to do any manipulation with the collection like Dataset and other data sources, use IQueryable.

In simple words other major difference is that IEnumerable execute select query on server side, load data in-memory on client side and then filter data while IQueryable execute select query on server side with all filters. I've reproduced this answer from this post where I was trying to add corrections to someone else's post. Now, my goal was to simply get top 2 records from Employee table in database. I added an ADO.

It is just that IQueryable is smart enough to apply the Top 2 clause on database server side itself so it brings only 2 out of 5 records over the wire. Any further in-memory filtering is not required at all on client computer side.

Now the thing is IEnumerable brought all the 5 records present in Salary table and then performed an in-memory filtering on the client computer to get top 2 records. So more data 3 additional records in this case got transferred over the wire unnecessarily.

IEnumerable is refering to a collection but IQueryable is just a query and it will be generated inside a Expression Tree. We use IEnumerable and IQueryable to manipulate the data that is retrieved from database. The major difference between IQueryable and IEnumerable is that IQueryable executes query with filters whereas IEnumerable executes the query first and then it filters the data based on conditions.

So IEnumerable is generally used for dealing with in-memory collection, whereas, IQueryable is generally used to manipulate collections. Here is what I wrote on a similar post on this topic. And no, I don't usually quote myself, but these are very good articles. EDIT: Removed a link to an article that was once helpful but now redirects to a site with porn.

Thanks to Grace for pointing this out! Instead, their functionality is to build an Expression object, which is an expression tree that represents the cumulative query.

Expression trees are a very important construct in C and on the. NET platform. They are important in general, but C makes them very useful. To better understand the difference, I recommend reading about the differences between expressions and statements in the official C 5. For advanced theoretical concepts that branch into lambda calculus, expressions enable support for methods as first-class objects. There are many different types of Expressions, and each Expression has specific rules for how that Expression type gets mapped to some SQL statement.

Which is a valid SQL statement! I hope this article was helpful in helping you understand the difference between the IEnumerable and IQueryable interfaces are. I hope you enjoy this and the rest of the articles published during the C Advent! Thanks to Matt Groves for setting this up again this year!

IEnumerable: IQueryable: Since the methods available are basically the same, what really sets these two interfaces apart? IEnumerable — intended to be run against an in memory collection. Lets take the following examples.

Source Code Lets take a look at the implementation for a where statement for the IEnumerable and IQueryable interfaces. Simple Query Provider To see the basic workings of a query provider lets go ahead and create a very minimal query provider. Lets start off by taking a look at the IQueryable interface.

Lets start coding by creating a Query class that will implement the IQueryable interface. So lets turn our attention to our Query Provider.



0コメント

  • 1000 / 1000