IEnumerable vs IQueryable. Know the difference

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

In this article, you are going to cover when to use IEnumerable and IQueryable when you manipulate different data sources. IEnumerable is useful when you are loading the data through LINQ or Entity Framework and want to apply filters on the data. IQueryable is inherited from IEnumerable; therefore, it has all the functionalities of the parent interface. IEnumerable works with in-memory collection whereas IQueryable works with remote data sources.

What is IEnumerable?

IEnumerable is an interface that allows us to implement the GetEnumerator method, which provides a read-only access to the data collection. IEnumerable interface is a generic interface that allows you to apply looping over generic and non-generic collection of data.

How to Implement IEnumerable in C#

In C#, you can implement IEnumerable interface for in-memory data objects.

Let us assume that you need to create a simple application to fetch the demographic information of walk-in patients in the hospital and you need those patients who are 50 years old or more. You can filter the results with the help of following code:

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestApp
{ 
   public class Patient
   {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }

       public IEnumerable GetPatients()
       {
            List patientList = new List()
            {
                new Patient (){ID = 1, Name = "John", Gender = "Male", Age = 20},
                new Patient (){ID = 2, Name = "Hanah", Gender = "Female", Age = 32},
                new Patient (){ID = 3, Name = "Timmy", Gender = "Male", Age = 55},
                new Patient (){ID = 4, Name = "Bella", Gender = "Female", Age = 68}
            };
       }
   }

    public class Program
    {
        public static void Main(string[] args)
        { 
            // initialize Patient class 
            Patient objPatient = new Patient();

            // Get Patients List 
            IEnumerable patientList = new List();
            patientList  = objPatient.GetPatients();

            //Use LINQ Query to get patient list who are above 50 years
            IEnumerable filterList = from obj in patientList
                                               where obj.Age >= 50
                                               select obj;

            //Iterate through the collection
            foreach (var patient in filterList)
            {
                Console.WriteLine( $"Patient ID : {patient.ID}  Patient Name : {patient.Name}");
            }
            Console.ReadKey();        
      }
    }
}

What is IQueryable?

IQueryable is inherited from IEnumerable interface, which makes it achieve all the functionality of its parent interface. Now let us take the same example, but this time with the IQueryable provider to iterate over the data collection.


using System;
using System.Collections.Generic;
using System.Linq;

namespace TestApp
{ 
   public class Patient
   {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }

        public IEnumerable GetPatients()
       {
            List patientList = new List()
            {
                new Patient (){ID = 1, Name = "John", Gender = "Male", Age = 20},
                new Patient (){ID = 2, Name = "Hanah", Gender = "Female", Age = 32},
                new Patient (){ID = 3, Name = "Timmy", Gender = "Male", Age = 55}
            };
       }

   }

     public class Program
    {
        public static void Main(string[] args)
        { 
            // initialize Patient class 
            Patient objPatient = new Patient();

           // Get Patients List 
           IEnumerable patientList = new List();
           patientList  = objPatient.GetPatients();

            //Use LINQ Query to get patient list who are above 50 years
            IQueryable filterList = patientList.AsQueryable()
                                .Where(obj => obj.Age >= 50);

            //Iterate through the collection
            foreach (var patient in filterList)
            {
                Console.WriteLine( $"Patient ID : {patient.ID}  Patient Name : {patient.Name}");
            }
            Console.ReadKey();        
      }
    }
}

IEnumerable and IQueryable Properties

Namespace

  • IEnumerable is derived from System.Collections.
  • IQueryable is derived from System.LINQ.

     

    Parent Interface

    • IEnumerable is not inherited from any interface.
    • IQueryable is inherited from IEnumerable interface.

     

    Lazy Loading

    • IEnumerable does not support lazy loading.
    • IQueryable supports lazy loading of the data.

     

    Query Execution

    • IEnumerable executes query on server side, loads the data in memory on client side, and applies filter.
    • IQueryable executes query on server side with all the filters which makes it more efficient than IEnumerable.

     

    Custom Query

    • IEnumerable does not support any custom query.
    • IQueryable supports in creating custom queries using CreateQuery and Execute

     

    Query Usage

    • IEnumerable is used in LINQ to Object and LINQ to XML queries.
    • IQueryable is used in LINQ to SQL queries.

     

    When to use

    • IEnumerable is used in querying data from in-memory collections like Arrays, List etc.
    • IQueryable is used in querying data from out-memory collections like remote database, services etc.
    Facebook
    Twitter
    LinkedIn

    Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.