The principle difference is that IEnumerable will enumerate all of its elements all the time, while IQueryable will enumerate elements, or even do other things, based on a query. The query is an Expression (a data representation of .
IQueryable is faster than IEnumerable. In addition to Munesh Sharma's answer:IEnumerable loads data in-memory and then apply filters to it one by one but IQueryable apply filters all at once and return the result.
IEnumerable is useful when we want to iterate the collection of objects which deals with in-process memory. IQueryable is useful when we want to iterate a collection of objects which deals with ad-hoc queries against the data source or remote database, like SQL Server.
The main difference between IEnumerable and IQueryable in C# is that IQueryable queries out-of-memory data stores, while IEnumerable queries in-memory data.
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, yield
ing 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).
See:
In real life, if you are using a ORM like LINQ-to-SQL
IQueryable
, then the query may be converted to sql and run on the database serverIEnumerable
, then all rows will be pulled into memory as objects before running the query.In both cases if you don't call a ToList()
or ToArray()
then query will be executed each time it is used, so, say, you have an IQueryable
and you fill 4 list boxes from it, then the query will be run against the database 4 times.
Also if you extend your query:
q.Select(x.name = "a").ToList()
Then with an IQueryable
the generated SQL will contain where name = "a"
, but with an IEnumerable
many more roles will be pulled back from the database, then the x.name = "a"
check will be done by .NET.
"The primary difference is that the extension methods defined for IQueryable take Expression objects instead of Func objects, meaning the delegate it receives is an expression tree instead of a method to invoke. IEnumerable is great for working with in-memory collections, but IQueryable allows for a remote data source, like a database or web service"
Source: here
IEnumerable IEnumerable is best suitable for working with in-memory collection. IEnumerable doesn’t move between items, it is forward only collection.
IQueryable IQueryable best suits for remote data source, like 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).
So when you have to simply iterate through the in-memory collection, use IEnumerable, if you need to do any manipulation with the collection like Dataset and other data sources, use IQueryable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With