IEnumerable 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.
IQueryable implements the same LINQ standard query operators, but accepts Expression> for predicates and anonymous functions. Expression 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:
Code
IEnumerable people = GetEnumerablePeople(); Person person = people.Where(x => x.Age > 18).FirstOrDefault(); IQueryable people = GetQueryablePeople(); Person person = people.Where(x => x.Age > 18).FirstOrDefault();
In the first block, x => x.Age > 18 is an anonymous method (Func), 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>), 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, 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).
The major difference between these two is that IEnumerable<T> is in memory collection while IQueryable<T> executes the query on the database server and then fetches records, if you don't cast it to IEnumerbale<T>, you can futher apply more queries on that IQueryable<T>, for example, filtering, sorting, grouping. For IEnumerable<T> these all operation will be done in memory while IQueryable<T> translates it to database query and then executes it on server.
Vinay SinghRank #604 · 4K reputationPosted Jun 28, 2016, 10:23 PM EDT
IQueryable :
scropio gurl145 reputationPosted Jun 28, 2016, 10:27 PM EDT
IQueryable and Code
Vinay SinghRank #604 · 4K reputationPosted Jun 28, 2016, 10:34 PM EDT
IEnumerablerepresents a forward-only cursor ofT. .NET 3.5 added extension methods that included theLINQ standard query operatorslikeWhereandFirst, with any operators that require predicates or anonymous functions takingFunc.IQueryableimplements the same LINQ standard query operators, but acceptsExpression> for predicates and anonymous functions.Expressionis 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:
In the first block,
x => x.Age > 18is an anonymous method (Func), which can be executed like any other method.Enumerable.Wherewill execute the method once for each person,yielding values for which the method returnedtrue.In the second block,
x => x.Age > 18is an expression tree (Expression> ), 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
IQueryableis enumerated (it implementsIEnumerable, after all), it can combine multiple query operators (in the above exampleWhereandFirstOrDefault) to make smarter choices on how to execute the entire query against the underlying data source (like usingSELECT TOP 1in SQL).See:
scropio gurl145 reputationPosted Jun 28, 2016, 10:47 PM EDT
Vinay SinghRank #604 · 4K reputationPosted Jun 28, 2016, 11:10 PM EDT
scropio gurl145 reputationPosted Jun 28, 2016, 11:17 PM EDT
scropio gurl145 reputationPosted Jun 28, 2016, 11:22 PM EDT
scropio gurl145 reputationPosted Jun 28, 2016, 11:58 PM EDT
Ehsan SajjadRank #512 · 4.8K reputationPosted Jul 1, 2016, 2:05 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Jul 1, 2016, 2:32 AM EDT
scropio gurl145 reputationPosted Jul 1, 2016, 10:24 PM EDT
Ehsan Sajjad
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts