.NET Technical bits: IEnumerable
Showing posts with label IEnumerable. Show all posts
Showing posts with label IEnumerable. Show all posts

Monday, May 10, 2010

IEnumerable vs. IQueryable

IQueryable

• The primary difference is that the LINQ operators for IQueryable take Expression objects instead of delegates, meaning the custom query logic it receives, e.g., a predicate or value selector, is in the form of an expression tree instead of a delegate to a method.
• IQueryable extends the IEnumerable interface, so anything you can do with a "plain" IEnumerable, you can also do with an IQueryable.
• The expression can simply be a constant expression of the object itself or a more complex tree of a composed set of query operators and operands. The query provider's IQueryProvider.Execute() or IQueryProvider.CreateQuery() methods are called with an Expression passed to it, and then either a query result or another IQueryable is returned, respectively.
• IQueryable allows for out-of memory things like a remote data source, such as a database or web service.
• IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).

IEnumerable

• IEnumerable doesn’t have the concept of moving between items, it is a forward only collection. It’s very minimalistic; something that most any data source can provide. Using only this minimal functionality, LINQ can provide all of these great operators.
• IEnumerable is great for working with sequences that are iterated in-memory.
• IEnumerable just has a GetEnumerator() method that returns an Enumerator for which you can call its MoveNext() method to iterate through a sequence of T

LINQ Queries - Query Syntax

The recommended way to write most queries is to use query syntax to create query expressions. The following example shows three query expressions. The first query expression demonstrates how to filter or restrict results by applying conditions with a where clause. It returns all elements in the source sequence whose values are greater than 7 or less than 3. The second expression demonstrates how to order the returned results. The third expression demonstrates how to group results according to a key. This query returns two groups based on the first letter of the word.

// Query #1.
List numbers = new List() { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

// The query variable can also be implicitly typed by using var
IEnumerable filteringQuery =
from num in numbers
where num < 3 || num > 7
select num;

// Query #2.
IEnumerable orderingQuery =
from num in numbers
where num < 3 || num > 7
orderby num ascending
select num;

// Query #3.
string[] groupingQuery = { "carrots", "cabbage", "broccoli", "beans", "barley" };
IEnumerable<IGrouping> queryFoodGroups =
from item in groupingQuery
group item by item[0];

Note that the type of the queries is IEnumerable<(Of <(T>)>). All of these queries could be written using var as shown in the following example:
var query = from num in numbers...
In each previous example, the queries do not actually execute until you iterate over the query variable in a foreach statement.