IEnumerable vs. IQueryable

"How to Use" IEnumerable and IQueryable [IEnumerable vs. IQueryable]

Both the IEnumerable and IQueryable interfaces are for .NET collections, so if you are new please see this article and the IQueryable interface inherits from IEnumerable interface, so whatever IEnumerable can do, IQueryable can also do.

The IQueryable interface is the same as IEnumerable interface  but it also provides additional functionality to implement custom querying with LINQ.

Stayed Informed - IEnumerable vs. IList

There are some differences over the interfaces IEnumerable and IQueryable as given below.

IEnumerable:-

The IEnumerable<T> interface exists in the namespace “System.Collections”.

The IEnumerable<T> represents a forward only cursor of T.

The IEnumerable<T> interface is fit for querying data from in memory collections like Array, List, and collations etc.

While us querying data from the database. The IEnumerable executes SELECT Query on the server side, load list of the data in memory on the client side and after that apply filters on the data.

In other word, if you create an IEnumerable then all the rows will be pulled into memory as objects before running the query filters.

The IEnumerable<T> interface is more useful for LINQ to XML and LINQ to object queries.

The IEnumerable<T> interface is best suitable for in-memory collection. It doesn’t move between items. It is forward only collection.

The IEnumerable<T> interface has a method GetEnumerator() and returns an Enumerator<T>, which we can call its method MoveNext() to iterate through a sequence of T.

For Example,
CustEntites entitesObj = new CustEntites();  //This is entities objects.
IEnumerable<Customers> customer = entitesObj.Customers.Where(x => x.CusId > 0).ToList<Customers>();
IQueryable:-

The IQueryable<T> interface exists in the Namespace “System.Linq”.

The IQueryable<T> interface implements same like LINQ standard query operators but accepts as predicates and anonymous functions.

The IQueryable <T> interface is fit for querying data from out-of memory collections like database or web services.

While we querying data from a database, the IQueryable executes a SELECT query on the server-side database with the filters.

The IQueryable <T> interface is an expression tree parsing.

The IQueryable<T> interface is more beneficial for LINQ to SQL queries.

The IQueryable<T> interface is best use for remote data sources like a web service etc. It is a very powerful feature that enables paging, shorting, filtering based on queries.

For Example,
CustEntites entitesObj = new CustEntites();  //This is entities objects.
IEnumerable<Customers> customer = entitesObj.Customers.Where(x => x.CusId > 0).ToList<Customers>();

Stayed Informed - IEnumerable vs. IList

I hope you are enjoying with this post! Please share with you friends. Thank you!!
ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book