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 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.
     IEnumerable interface

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.
    Entity Framework

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.
    IQueryable

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.

To understand the time consumed by both IEnumerable & IQueryable let's print the time taken by both.

IEnumerable & IQueryable

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("Employees with salaries > 45000");

    foreach (var item in employees)
    {
        Console.WriteLine("Name: " + item.FirstName + ", Salary: " + item.Salary);
    }
}

Output

The time taken by IEnumerable is 634 milliseconds.

IEnumerable 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("Employees with salaries > 45000");

    foreach (var item in employees)
    {
        Console.WriteLine("Name: " + item.FirstName + ", Salary: " + item.Salary);
    }
}

Output

The time taken by IQueryable is 325 milliseconds, almost half of IEnumerable.

Output

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 transferred through the network?

Conclusion

In this article, we learned,

  • What is IQuerable & how to use it?
  • 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 queries, you can ping me @


Similar Articles