Cloud Training (Online)

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>();
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.