HomeCSharpWhat is the difference between IEnumerable and IQueryable in C# ?

What is the difference between IEnumerable and IQueryable in C# ?

If you are a C# developer and work with collections as well as LINQ/Entity Framework , you would have come across these 2 interfaces

  • IEnumerable
  • IQueryable

What is the difference between IEnumerable and IQueryable in C# ?

1. IEnumerable is used mostly for working with in-memory collection data where as IQueryable suits best when working with remote data sources or database.

2. IEnumerable is defined in the System.Collections Namespace whereas IQueryable is defined in the System.Linq Namespace.

3. IQueryable extends the IEnumerable interface . The IEnumerable has a GetEnumerator() method.

IEnumerable vs IQuerable in C#

IEnumerable vs IQuerable in C#

4. IQueryable supports lazy loading and can be used for paging scenarios where as IEnumerable cannot be.

Below is a sample example demonstrating the IEnumerable and IQueryable .

List<Movie> movies = new Movies();

IQueryable<Movie> moviesLinq1 = (from m in movies

orderby m.Actor , m.MovieName

select m).AsQueryable<Movie>();

IEnumerable<Movie> moviesLinq2 = (from m in movies

orderby m.Actor, m.MovieName

select m).AsEnumerable<Movie>();

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...