Introduction
In this article we will learn what is the difference between IEnumerable and IQueryable in c#. What is IEnumerable and IQueryable. When to use IQueryable and when to use IEnumerable in c#. What is the use of Ienumerable and IQueryable in C# also IEnumerable and IQueryable difference in C#.
Previous Updates
In previous articles we have learnt AngularJS basics, Ransomware attack and Precautions, Difference between Row_Number(), Rank(), Rank_Density() with example.
Const Readonly and Static difference in C#, Best basic C# interview questions.
Why do we need IEnumerable and IQueryable
Both IEnumerable and IQueryable are used for to hold the collection of a data and performing data manipulation operation for example filtering on the collection of data.
IEnumerable
1. BY using System.Collection namespace we can add access or use the IEnumerable in our code.
2. IEnumerable is much slower in processing because when selecting data from database it executes select query on server side, load data in-memory on client side and then filter data.
For Example you have a Employee table with lacks of records and you need to select all the records which having salary less then 10,000. Selecting records using IEnumerable then it select all the records from the table and load all of them into memory after that it will perform filtration and will searched for Employee less then 10,000 salary.
DataClasses1DataContext dbcon = new DataClasses1DataContext();
IEnumerable<EmployeeDetail> emplist = dbcon.EmployeeDetails.Where
(e => e.Salary < 10000 );
|
3. IEnumerable is only preferable for small data manipulations.
4. Best use of IEnumerable is with LINQ to Objects and LINQ to XML operations.Because it load all the data in memory the performs any operation.
IQueryable
1. BY using System.Linq namespace we can add access or use the IQueryable in our code.
2. IQueryable is much faster in processing because it do not load all the data in memory.
For Example you have a Employee table with lacks of records and you need to select all the records which having salary less then 10,000. Performing SELECT query on table using IQueryable then it select only filtered record from the table(Gets only Salary <10000 Employess). It will not load all the records into memory. That's why it is fast.
DataClasses1DataContext dbcon = new DataClasses1DataContext();
IEnumerable<EmployeeDetail> emplist = dbcon.EmployeeDetails.Where
(e => e.Salary < 10000 );
|
3. By using IQueryable you can perform big data manipulation tasks.
4. IQueryable is used in LINQ To SQL Queries.
|
IEnumerable
|
IQueryable
|
Namespace used
|
System.Collections
|
System.Linq
|
Derived Interface
|
No base interface
|
Derives from IEnumerable
|
Lazy Loading
|
Not Supported.
|
Supported
|
Best Suitable For
|
LINQ to Object and LINQ to XML queries.
|
LINQ to SQL queries.
|
Custom Query
|
Doesn’t supports.
|
Supports using CreateQuery and Execute methods.
|
When To Use
|
Working with the read only collection.
Need to read the objects in forward direction only.
Not concerned about thread safety.
Want to iterate the collection’s objects using foreach.
|
Working with the queryable datasource.
Need to apply filter on data at the datasource.
Need to apply paging , composition.
Working with external data source.
Needs to load data in deferred way.
Need to use foreach to iterate collection.
|
0 comments:
Post a Comment