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

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...