Introduction
Today let's try to understand why we have Iqueryable in C# when IEnumerable can get the work done.
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 SQLServer 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 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 been filtered.
It consumes less time, as it transfers only filtered records through a network.
Let's see this in action.
Let's check what do we have in the database
8 records, we will filter these records based on salary. Fetch records with a salary greater than 45000
To understand the time consumed by both IEnumerable & IQueryable let's print the time taken by both
1st IEnumerable:
- using (var db = new EmployeeDBEntities())
- {
- DateTime now = DateTime.Now;
-
- IEnumerable<Employee> employees= from emp in db.Employees
- where emp.Salary > 45000
- orderby emp.Salary
- select emp;
- Console.WriteLine("Time taken by IEnumerable:" + (DateTime.Now - now).Milliseconds + " Milliseconds");
- Console.WriteLine("Employess with salaries > 45000");
- foreach (var item in employees)
- {
- Console.WriteLine("Name: "+item.FirstName + ", Salary: " + item.Salary);
- }
- }
Output
Time taken by IEnumerable is 634 milliseconds.
Now let's filter data with IQueryable.
- using (var db = new EmployeeDBEntities())
- {
- DateTime now = DateTime.Now;
-
- IQueryable<Employee> employees= from emp in db.Employees
- where emp.Salary > 45000
- orderby emp.Salary
- select emp;
- Console.WriteLine("Time taken by IQueryable:" + (DateTime.Now - now).Milliseconds + " Milliseconds");
- Console.WriteLine("Employess with salaries > 45000");
- foreach (var item in employees)
- {
- Console.WriteLine("Name: "+item.FirstName + ", Salary: " + item.Salary);
- }
- }
Output
Time taken by IQueryable is 325 milliseconds, almost half of IEnumerable.
Note
The difference between this time depends on a lot of factors.
- How fast is your network bandwidth?
- How many filters have been applied?
- How many records are transferring through the network?
Conclusion
In this article, we learned:
- What is IQuerable & how to use?
- Where to use IQueryable.
- What is the difference between Iqueryable & IEnumerable?
Thanks a lot. I hope this article was helpful to all of you.
If you have any query, you can ping me @