IQueryable vs IEnumerable

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 dbfirst-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 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:
  1. using (var db = new EmployeeDBEntities())  
  2.           {  
  3.               DateTime now = DateTime.Now;  
  4.                 
  5.               IEnumerable<Employee> employees= from emp in db.Employees  
  6.                                               where emp.Salary > 45000  
  7.                                               orderby emp.Salary  
  8.                                               select emp;  
  9.               Console.WriteLine("Time taken by IEnumerable:" + (DateTime.Now - now).Milliseconds + " Milliseconds");  
  10.               Console.WriteLine("Employess with salaries > 45000");  
  11.               foreach (var item in employees)  
  12.               {  
  13.                   Console.WriteLine("Name: "+item.FirstName + ",     Salary: " + item.Salary);  
  14.               }  
  15.            } 
Output
 
Time taken by IEnumerable is 634 milliseconds.
 
 
Now let's filter data with IQueryable.
  1. using (var db = new EmployeeDBEntities())  
  2. {  
  3.     DateTime now = DateTime.Now;  
  4.       
  5.     IQueryable<Employee> employees= from emp in db.Employees  
  6.                                     where emp.Salary > 45000  
  7.                                     orderby emp.Salary  
  8.                                     select emp;  
  9.     Console.WriteLine("Time taken by IQueryable:" + (DateTime.Now - now).Milliseconds + " Milliseconds");  
  10.     Console.WriteLine("Employess with salaries > 45000");  
  11.     foreach (var item in employees)  
  12.     {  
  13.         Console.WriteLine("Name: "+item.FirstName + ",     Salary: " + item.Salary);  
  14.     }  
  15.  } 
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 @


Similar Articles