Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Var and IEnumerable with LINQ

  Author : Shailendra Chauhan
Updated On : 24 Sep 2016
Total Views : 66,460   
 

In previous article I explain the difference between IEnumerable and IQuerable, IEnumerable and IList. In this article I would like to share my opinion on Var and IEnumerable with LINQ. IEnumerable is an interface that can move forward only over a collection, it can’t move backward and between the items. Var is used to declare implicitly typed local variable means it tells the compiler to figure out the type of the variable at compilation time. A var variable must be initialized at the time of declaration. Both have its own importance to query data and data manipulation.

Var Type with LINQ

Since Var is anonymous types, hence use it whenever you don't know the type of output or it is anonymous. In LINQ, suppose you are joining two tables and retrieving data from both the tables then the result will be an Anonymous type.

 var q =(from e in tblEmployee 
 join d in tblDept on e.DeptID equals d.DeptID 
 select new 
 { 
 e.EmpID,
 e.FirstName,
 d.DeptName,
 }); 

In above query, result is coming from both the tables so use Var type.

 var q =(from e in tblEmployee where e.City=="Delhi" select new { 
 e.EmpID,
 FullName=e.FirstName+" "+e.LastName, 
 e.Salary 
}); 

In above query, result is coming only from single table but we are combining the employee's FirstName and LastName to new type FullName that is annonymous type so use Var type. Hence use Var type when you want to make a "custom" type on the fly.

More over Var acts like as IQueryable since it execute select query on server side with all filters. Refer below examples for explanation.

IEnumerable Example

 MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10); 

Generated SQL statements of above query will be :

 SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0

Notice that in this query "top 10" is missing since IEnumerable filters records on client side

Var Example

 MyDataContext dc = new MyDataContext ();
var list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10); 

Generated SQL statements of above query will be :

 SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0

Notice that in this query "top 10" is exist since var is a IQueryable type that executes query in SQL server with all filters.

IEnumerable Type with LINQ

IEnumerable is a forward only collection and is useful when we already know the type of query result. In below query the result will be a list of employee that can be mapped (type cast) to employee table.

 IEnumerable<tblEmployee> lst =(from e in tblEmployee 
where e.City=="Delhi" 
select e); 

Note

  1. In LINQ query, use Var type when you want to make a "custom" type on the fly.

  2. In LINQ query, use IEnumerable when you already know the type of query result.

  3. In LINQ query, Var is also good for remote collection since it behaves like IQuerable.

  4. IEnumerable is good for in-memory collection.

Summary

In this article, I explain when to use Var and IEnumerable with LINQ while writing your LINQ query. I hope after reading this article you will be able to do this. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+