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>();
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply