Header add


In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation. In this article we will discuss
>> What is IEnumerable in C#?
>> What is IQueryable in C# ?
>> Examples of IEnumerable and IQueryable in C#
>> Difference between IEnumerable and IQueryable in C#

What is IEnumerable in C# with example ?

Many times there is a need to loop through a collection of classes or lists which are anonymous types. IEnumerable interface is one of the best features of C# language which loops over the collection.

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.

IEnumerable is an interface that is available in System.Collection namespace.The IEnumerable interface has also a child generic interface i.e. IEnumerable<T>

All the collection classes (both generic and non-generic) implements the IEnumerable interface. Let us proof this by visiting the definition of List<T> generic collection class as shown in the below image.

Now let us see the definition of ArrayList collection which is a non-generic collection class.

Some key points need to remember about IEnumerable  interface.
  • IEnumerable exists in System.Collections Namespace.
  • IEnumerable can move forward only over a collection, it can’t move backward and between the items.
  • IEnumerable is best to query data from in-memory collections like List, Array, etc.
  • While query data from a database, IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data.
  • IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
  • IEnumerable supports deferred execution.
  • IEnumerable doesn’t support custom query.
  • IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
  • Extension methods support by IEnumerable takes functional objects.
IEnumerable Example in C#
    namespace LINQSeries  
     {  
       public class Program  
       {  
         static void Main(string[] args)  
         {  
           List<Employee> employeeList = new List<Employee>()  
           {  
             new Employee(){empID = 1, empName = "John", Gender = "Male",Department="HR"},  
             new Employee(){empID = 2, empName = "Lori", Gender = "Female",Department="IT"},  
             new Employee(){empID = 3, empName = "Glen", Gender = "Male",Department="HR"},  
             new Employee(){empID = 4, empName = "Rodie", Gender = "Female",Department="IT"},  
             new Employee(){empID = 5, empName = "Sweta", Gender = "Female",Department="HR"}  
           };  
           //Linq Query to Fetch all Employee with Department as IT  
           IEnumerable<Employee> QuerySyntax = from emp in employeeList  
                             where emp.Department == "IT"  
                             select emp;  
           //Iterate through the collection  
           foreach (var employee in QuerySyntax)  
           {  
             Console.WriteLine($"ID : {employee.empID} Name : {employee.empName} " +  
               $"Gender: { employee.Gender} Department: { employee.Department}");  
           }  
           Console.ReadLine();  
         }  
       }  
       public class Employee  
       {  
         public int empID { get; set; }  
         public string empName { get; set; }  
         public string Gender { get; set; }  
         public string Department { get; set; }  
       }  
     }  
After run the application you can see the employee list iterate and filter the department.

What is IQueryable in C# with example ? 

IQueryable is useful when we want to iterate a collection of objects which deals with ad-hoc queries against the data source or remote database, like SQL Server.


  • The IQueryable is an interface and it is available in System.Linq namespace. The IQuerable interface is a child of the IEnumerable interface. So we can store IQuerable in a variable of type IEnumerable
  • The IQuerable interface has a property called Provider which is of type IQueryProvider interface. Let us see the definition of IQueryProvider.
Some key points need to remember about IQueryable interface.
  • IQueryable exists in System. Linq Namespace.
  • IQueryable can move forward only over a collection, it can’t move backward and between the items.
  • IQueryable is best to query data from out-memory (like remote database, service) collections.
  • While query data from a database, IQueryable execute the select query on the server side with all filters.
  • IQueryable is suitable for LINQ to SQL queries.
  • IQueryable supports deferred execution.
  • IQueryable supports custom query using CreateQuery and Execute methods.
  • IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
  • Extension methods support by IQueryable takes expression objects means expression tree.
IQueryable Example in C#
    namespace LINQSeries  
     {  
       public class Program  
       {  
         static void Main(string[] args)  
         {  
           List<Employee> employeeList = new List<Employee>()  
           {  
             new Employee(){empID = 1, empName = "John", Gender = "Male",Department="HR"},  
             new Employee(){empID = 2, empName = "Lori", Gender = "Female",Department="IT"},  
             new Employee(){empID = 3, empName = "Glen", Gender = "Male",Department="HR"},  
             new Employee(){empID = 4, empName = "Rodie", Gender = "Female",Department="IT"},  
             new Employee(){empID = 5, empName = "Sweta", Gender = "Female",Department="HR"}  
           };  
           //Linq Query to Fetch all employee with Department  
           IQueryable<Employee> MethodSyntax = employeeList.AsQueryable()  
                     .Where(std => std.Department == "IT");  
           //Iterate through the collection  
           foreach (var employee in MethodSyntax)  
           {  
             Console.WriteLine($"ID : {employee.empID} Name : {employee.empName} " +  
               $"Gender: { employee.Gender} Department: { employee.Department}");  
           }  
           Console.ReadLine();  
         }  
       }  
       public class Employee  
       {  
         public int empID { get; set; }  
         public string empName { get; set; }  
         public string Gender { get; set; }  
         public string Department { get; set; }  
       }  
     }  
Now run the application and you will get same result as above. 
The point that you need to remember is in order to return a collection of type IQueryable, first, you need to call the AsQueryable() method on the collection

Difference between IEnumerable and IQueryable in C# ?


IEnumerable IQueryable
IEnumerable is an interface that is available in the System.Collections namespace.The IQueryable is an interface that exists in the System.Linq Namespace.
While querying the data from the database, the IEnumerable executes the “select statement” on the server-side (i.e. on the database), loads data into memory on the client-side and then only applied the filters on the retrieved data.While querying the data from a database, the IQueryable executes the “select query” with the applied filter on the server-side i.e. on the database and then retrieves data.
IEnumerable use when you need to query the data from in-memory collections like List, Array and so on.IQueryable use when you want to query the data from out-memory such as remote database, service, etc.
The IEnumerable is mostly used for LINQ to Object and LINQ to XML queries.IQueryable is mostly used for LINQ to SQL and LINQ to Entities queries.
The IEnumerable collection is of type forward only. That means it can only move in forward, it can’t move backward and between the items.The collection of type IQueryable can move only forward, it can’t move backward and between the items.
IEnumerable supports deferred execution.IQueryable supports deferred execution.
It doesn’t support custom queries.It also supports custom queries using CreateQuery and Executes methods.
The IEnumerable doesn’t support lazy loading. Hence, it is not suitable for paging like scenarios.IQueryable supports lazy loading and hence it is suitable for paging like scenarios.



Summary
    In this tutorial we discussed IEnumerable and IQueryable in C#. If have any question related to this topic then give your feedback.


Post a Comment

Previous Post Next Post