Difference between Var and IEnumerable      S.No    Var                               IEnumerable      1       When to use...
Reference:http://www.dotnet-tricks.com/Tutorial/linq/aPNK140912-Var-VS-IEnumerable.htmlAnd, further updates on difference ...
Upcoming SlideShare
Loading in …5
×

Var vs iEnumerable

813 views

Published on

Var vs iEnumerable

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
813
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Var vs iEnumerable

  1. 1. Difference between Var and IEnumerable S.No Var IEnumerable 1 When to use ? When to use ? Use Var type when we want to Use IEnumerable when we already make a "custom" type on the know the type of query result. fly. 2 Good for: Good for: Var is also good for remote IEnumerable is good for in-memory collection. collection.IEnumerable ExampleMyDataContext 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 @p0Notice that in this query "top 10" is missing since IEnumerable filters records on clientsideVar ExampleMyDataContext 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 @p0Notice that in this query "top 10" is exist since var is a IQueryable type that executesquery in SQL server with all filters.IEnumerable TypeIEnumerable is a forward only collection and is useful when we already know the type ofquery result. In below query the result will be a list of employee that can be mapped (typecast) to employee table.IEnumerable<tblEmployee> lst =(from e in tblEmployeewhere e.City=="Delhi"select e);
  2. 2. Reference:http://www.dotnet-tricks.com/Tutorial/linq/aPNK140912-Var-VS-IEnumerable.htmlAnd, further updates on difference between questions and answers, please visit myblog @ http://onlydifferencefaqs.blogspot.in/

×