IQueryable<T> vs IEnumerable<T>
A lots of time
Confusion between IQueryable and IEnumerable interface
because, they are look like same and when we start writing a code often, we
will choose a wrong approach between them. From behind this reason was I am
unable to know perfectly about these two. Here, we will try to explain a differences.
in System.Linq Namespace. It only forward only over a
collection. This is a better query data from out-memory (like remote database,
service) collections. When we access a data from a database IQueryable execute
select query on server side with all filters. It support Lazy Loading that was
better for paging.
IQueryable<T> is the interface that allows LINQ-to-SQL to
work. If we want to refine another query it will executes, if necessary.
stud = ………….
var studentGrade = db.Students.stud .Where(x =>x.grade<5);
That code will
execute SQL to only select Students which grade is <5. And we have also
filter all other students which grade above from 5.
in System.Collections Namespace. It only forward only
over a collection. IEnumerable is best to query data from in-memory
collections. While we access data from database, IEnumerable interface execute
select query on server side and load data in-memory on client side and then
filtering data. IEnumerable is suitable for LINQ to Object queries. IEnumerable
supports deferred execution. IEnumerable does not support custom query and also
be a lazy loading.
the interface that allows LINQ-to-Object to work. It means that all
objects matching the original query will have to be loaded into memory from the
stud = ……….
studentGrade = db.Students.stud .Where(x =>x.grade<5);
This is quite an
important difference, and working on IQueryable<T> can
in many cases save you from returning too many rows from the database. Another
prime example is doing paging: If you use Take andSkip on
IQueryable, you will only get the number of rows requested; doing that on an IEnumerable<T> will
cause all of your rows to be loaded in memory.
There are many
differences as below:
IEnumerable is exists under the System.Collections namespace.
suitable for querying data from in-memory collections.
3. While querying data from the database, IEnumerable executes
"select query" on the server-side, loads data in-memory on the
client-side and then filters the data.
4. IEnumerable is also be better work for LINQ to Object and LINQ to XML queries.
IQueryable is exists under the System.Linq Namespace.
IQueryable is suitable for querying data
from out-memory (like remote database, service) collections.
While querying data from a database,
IQueryable executes a "select query" on server-side with all filters.
is beneficial for LINQ to SQL queries.