Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C#

Into and Let in LINQ (Let vs. Into)

Rate me:
Please Sign up or sign in to vote.
3.41/5 (8 votes)
26 Jul 2011CPOL1 min read 105.8K   13   3
In this post, I am going to show two keywords in C# 3.0 which are very helpful when playing with a set of object collections using LINQ.

In this post, I am going to show two keywords in C# 3.0 which are very helpful when playing with a set of object collections using LINQ.

Into

The Into keyword allows creating a temporary variable to store the results of a group, join, or select clause into a new variable.

C#
var em = from e in emp
                      group e by new{ e.DeptId}
                          into gEmp 
                          where gEmp.Count() > 1
                       select new { gEmp.Key.DeptId, salary = gEmp.Sum(t => t.Salary) };

In the above query, after applying into on grouping, it creates a IGrouping type gEmp variable, which is used to apply the next filter.

Note: Into is used when you want to perform an operation on grouped data.

Let

The Let keyword allows storing the results of a query which can be used in a subsequent query; i.e., it creates a new variable and initializes it with the result of the expression you supply.

C#
var em = from e in emp
                     group e by new { e.Salary, e.Id }
                         into gEmp
                         let avgsal = (gEmp.Sum(t => t.Salary) / gEmp.Count())
                         where gEmp.Key.Salary == avgsal
                         select new { gEmp.Key.Salary, gEmp.Key.Id };

The above query is used to find out employee(s) having salary more than avgSalary. The Let keyword allows to create a new variable avgsal that is used in further operations.

Let vs. Into

Most people find it difficult to decide which one to use when designing a LINQ query.

  • Into – Hides the previous variable when used in a query, as you see in the above example. Which means it hides the previous range variable and creates a temporary range variable which you can use in further operations.
  • Let – Doesn’t hide the previous variable and creates a new variable. Which means you create a new variable and you can also use the previous variable, so you can use both in further operations.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
GeneralMy vote of 2 Pin
BillWoodruff24-Jan-15 23:22
professionalBillWoodruff24-Jan-15 23:22 
QuestionThe second LINQ has some issues. Pin
taocai20-Jan-14 18:03
taocai20-Jan-14 18:03 
AnswerRe: The second LINQ has some issues. Pin
Member 238763118-Aug-14 23:26
Member 238763118-Aug-14 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.