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


x

Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...