Click here to Skip to main content
Click here to Skip to main content

Into and Let in LINQ ( Let vs. Into)

By , 26 Jul 2011
 

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.

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.

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 its 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)

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India
Member

Microsoft C# MVP

 
Hey, I am Pranay Rana, working as a ITA in 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:



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid