What is the difference between IEnumerable and IQueryable?
IEnumerable and IQueryable may seem like the same at a
quick glance, but there are important differences that us, as developers must
be aware so that we not only get a working product out the door but also
something that performs well.
IEnumerable
|
IQueryable
|
·
IEnumerable exists in the System. Collections
namespace.
|
·
IQueryable exists in the
System.Linq Namespace.
|
·
IEnumerable is suitable for querying data
from in-memory collections like List, Array and so on.
·
Example:
IEnumerable<Emp>
emp = ent.Emps;
IEnumerable<Emp> temp = emp.Where(x
=> x.Empid ==5).ToList<Emp>();
This where filter is executed on the client
side where the “IEnumerable” code is. In other words, all the data is fetched
from the database and then at the client it scans and gets the record with
“EmpId” is “5”.
|
·
IQueryable is suitable for querying data from
out-memory like remote database collections.
·
Example : In this case, the filter
is applied on the database using the “SQL” query. So the client sends a
request and on the server side, a
select query is fired on the database and only
necessary data is returned. |
·
IEnumerable not supported Lazy Loading .
Hence not suitable for paging like scenarios.
|
·
IQueryable supported Lazy Loading .
|
·
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.
|
·
While querying data from a database,
IQueryable executes a "select query" on server-side with all
filters.
|
·
IEnumerable is beneficial for LINQ to Object
and LINQ to XML queries.
|
·
IQueryable is beneficial for LINQ to SQL
queries.
·
IQueryable
|
0 comments :
Post a Comment