Back to Questions

What's the difference between IQueryable and IEnumerable?

Your Answer

  • 12/17/2012 1:40:47 PM
  • Biju
  • 2

What's the difference between IQueryable and IEnumerable?

IEnumerable<T> represents a forward-only cursor of T. .NET 3.5 added extension methods that included the LINQ standard query operators like Where and First, with any operators that require predicates or anonymous functions taking Func<T>.

IQueryable<T> implements the same LINQ standard query operators, but accepts Expression<Func<T>> for predicates and anonymous functions. Expression<T> is a compiled expression tree, a broken-up version of the method ("half-compiled" if you will) that can be parsed by the queryable's provider and used accordingly.

For example:

IEnumerable<Person> people = GetEnumerablePeople();
Person person = people.Where(x => x.Age > 18).FirstOrDefault();

IQueryable<Person> people = GetQueryablePeople();
Person person = people.Where(x => x.Age > 18).FirstOrDefault();

In the first block, x => x.Age > 18 is an anonymous method (Func<Person, bool>), which can be executed like any other method. Enumerable.Where will execute the method once for each person, yielding values for which the method returned true.

In the second block, x => x.Age > 18 is an expression tree (Expression<Func<Person, bool>>), which can be thought of as "is the 'Age' property > 18".

This allows things like LINQ-to-SQL to exist because they can parse the expression tree and convert it into equivalent SQL. And because the provider doesn't need to execute until the IQueryable is enumerated (it implements IEnumerable<T>, after all), it can combine multiple query operators (in the above example Where and FirstOrDefault) to make smarter choices on how to execute the entire query against the underlying data source (like using SELECT TOP 1 in SQL).

Referance: http://stackoverflow.com/questions/2433306/whats-the-difference-between-iqueryable-and-ienumerable

 

  • 12/17/2012 1:44:22 PM
  • test

What's the difference between IQueryable and IEnumerable?

The principle difference is that IEnumerable will enumerate all of its elements all the time, while IEqueryable will enumerate elements, or even do other things, based on a query. The query is an Expression (a data representation of .Net code), which an IQueryProvider must explore/interpret/compile/whatever in order to generate results.

Having a query expression gives two advantages.

The first advantage is optimization. Because modifiers like 'Where' are included in the query expression, the IQueryProvider can apply otherwise impossible optimizations. Instead of returning all elements then throwing away most of them due to a 'Where' clause, the provider could use a hash table to locate items with a given key.

The second advantage is flexibility. Because Expressions are explorable data structures, you can do things like serialize the query and send it to a remote machine (eg. linq-to-sql).

Reference: http://stackoverflow.com/questions/2433306/whats-the-difference-between-iqueryable-and-ienumerable

 

  • 12/17/2012 1:45:25 PM
  • test

Back to Questions