Introduction
Hi folks, recently we have covered IEnumerable vs IEnumerator.
Today let's try to understand why we have Iqueryable in C# when IEnumerable can get the work done.
Say we have DB mapped into our project, If you want to learn how to do that, then you can visit the first-approach step-by-step guide.
First, let's understand what IQueryable is,
- Iqueryable is an interface used to iterate through collections.
- It inherits the IEnumerable interface & can be found in System. Linq namespace.

There is a key difference between IQueryable & IEnumerable
IEnumerable fetches all the data from the SQL server then it applies filters over fetched data. Let's break down its flow from the client to the server.
- The client makes a call to fetch data with filters using Entity Framework with IEnumerable
- SQL Server receives a request, It returns all the data without applying any specified filters
- The client receives a response, then it applies filters on the client side.

The result of this is that it consumes more time as it transfers all the data without filtering through a network.
Where IQueryable fetches filtered records from SQLServer rather than fetching all the records altogether. Let's break-down its flow from the client to the server.
- The client makes a call to fetch data with filters using Entity Framework with IQueryable
- SQL Server receives a request, It returns all the data after applying all the specified filter
- The client receives a response as expected. Now the client won't worry about filtering the records as they are already filtered.

It consumes less time, as it transfers only filtered records through a network.
Let's see this in action.
Let's check what we have in the database
8 records, we will filter these records based on salary. Fetch records with a salary greater than 45000.




Anup HosurPosted Jun 22, 2020, 9:26 AM
Nice article. In the next article, you can include deferred execution and why IQueryable is used for DB operations? ExpressionTree vs Func delegate for extension method
Amit NaikPosted Jun 22, 2020, 6:55 AM
Nice article