inforakesha

A fine WordPress.com site

Var VS IEnumerable

Leave a comment

In previous article I explain the difference between IEnumerable and IQuerableIEnumerable and IList. In this article I expose the difference between Var and IEnumerable. 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

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

  1. var q =(from e in tblEmployee
  2. join d in tblDept on e.DeptID equals d.DeptID
  3. select new
  4. {
  5. e.EmpID,
  6. e.FirstName,
  7. d.DeptName,
  8. });

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

  1. var q =(from e in tblEmployee where e.City==”Delhi” select new {
  2. e.EmpID,
  3. FullName=e.FirstName+” “+e.LastName,
  4. e.Salary
  5. });

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

  1. MyDataContext dc = new MyDataContext ();
  2. IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith(“S”));
  3. list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

  1. 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

  1. MyDataContext dc = new MyDataContext ();
  2. var list = dc.Employees.Where(p => p.Name.StartsWith(“S”));
  3. list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

  1. 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

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.

  1. IEnumerable<tblEmployee> lst =(from e in tblEmployee
  2. where e.City==”Delhi”
  3. select e);

Note

  1. Use Var type when you want to make a “custom” type on the fly.
  2. Use IEnumerable when you already know the type of query result.
  3. Var is also good for remote collection.
  4. IEnumerable is good for in-memory collection.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s