Use Of Into And Let In LINQ

Introduction

Here, we are going to see the uses of the Into and Let keywords in LINQ and we also will discuss their differences. Both keywords are helpful when playing with a set of object collections. Let's move to the detailed explanation.

Prerequisites
  • Visual Studio
  • Basic knowledge of LINQ
Article Flow  
  1. Use of Into Keyword
  2. Use of Let Keyword
  3. Difference between into and Let keyword 
Uses of Into

The Into keyword allows us to create a temporary variable to store the results of a group, join, or select clause into a new variable. Before moving on this, let's create a sample project. For this, I have created two classes and added dummy values in those.
  1. public class Employee {  
  2.     public int Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Designation {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public int YOE {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
  19. public class Salary {  
  20.     public int EmpID {  
  21.         get;  
  22.         set;  
  23.     }  
  24.     public double MonthSalary {  
  25.         get;  
  26.         set;  
  27.     }  
  28. }  
  29. class LinqSample {  
  30.     static void Main(string[] args) {  
  31.         List < Employee > empList = new List < Employee > ();  
  32.         empList.Add(new Employee {  
  33.             Id = 1, Name = "Gnanavel Sekar", Designation = "Software Engineer", YOE = 3  
  34.         });  
  35.         empList.Add(new Employee {  
  36.             Id = 2, Name = "Subash S", Designation = "Software Developer", YOE = 2  
  37.         });  
  38.         empList.Add(new Employee {  
  39.             Id = 3, Name = "Robert B", Designation = "Application Developer", YOE = 3  
  40.         });  
  41.         empList.Add(new Employee {  
  42.             Id = 4, Name = "Ammaiyappan k", Designation = "Software Engineer", YOE = 3  
  43.         });  
  44.         Console.WriteLine("Employee List");  
  45.         foreach(var employee in empList) {  
  46.             Console.WriteLine("ID:" + employee.Id + ", Name: " + employee.Name + ", Designation :" + employee.Designation + ", Year of Exp: " + employee.YOE);  
  47.         }  
  48.         List < Salary > salaryDetails = new List < Salary > ();  
  49.         salaryDetails.Add(new Salary {  
  50.             EmpID = 1, MonthSalary = 80000  
  51.         });  
  52.         salaryDetails.Add(new Salary {  
  53.             EmpID = 2, MonthSalary = 70000  
  54.         });  
  55.         salaryDetails.Add(new Salary {  
  56.             EmpID = 3, MonthSalary = 50000  
  57.         });  
  58.         salaryDetails.Add(new Salary {  
  59.             EmpID = 4, MonthSalary = 80000  
  60.         });  
  61.         Console.WriteLine(Environment.NewLine + "Salary Details");  
  62.         foreach(var salary in salaryDetails) {  
  63.             Console.WriteLine("Employee Id: " + salary.EmpID + ", Salary :" + salary.MonthSalary);  
  64.         }  
  65.     }  
  66. }  
Okay, now we see the clear list of employees and their respective salary.

 

Now, let us move into this concept by using these lists. First, make a simple example for easy understanding.
  1. var newEmployeeList = from e in empList  
  2. group e by new {  
  3.     e.YOE, e.Designation  
  4. }  
  5. into gEmployee  
  6. where gEmployee.Count() > 1  
  7. select new {  
  8.     gEmployee.Key.YOE, gEmployee.Key.Designation, totalemplcount = gEmployee.Count()  
  9. };  
  10. Console.WriteLine("New Grouped Employee List");  
  11. foreach(var emp in newEmployeeList) {  
  12.     Console.WriteLine("Designation : " + emp.Designation + ", Year of Emp : " + emp.YOE + ",Total Employee in this Group : " + emp.totalemplcount);  
  13. }  
Detailed Description
  • from e in empList -> we are assigning all collection values into the group e
  • group e by new{e.YOE, e.Designation} -> this group keyword is used to put the collection into one container of similar things with satified conditions, here we are grouping the employee by YOE (year of exp.,) and designation
  • into gEmployee -> grouped list or collection of values stored in temporary variable of gEmployee by using the into keyword where gEmployee.Count() > 1 -> Here, we are checking whether the grouped collection has more than one element.
  • select new { gEmployee.Key.YOE, gEmployee.Key.Designation, totalemplcount = gEmployee.Count() }; -> if more than one element exists in the temporary variable of collection, we proceed to select the value from that respective collection. Here, we are saying to select YOE, Designation, and total employee count in that respective group.

For ease of understanding, I have provided an image below.

 

To get more clear view, let's see another example.

  1. var newEmployeeSalary = (from t1 in empList join t2 in salaryDetails on t1.Id equals t2.EmpID group t2 by new {  
  2.         t2.MonthSalary  
  3.     }  
  4.     into g select new {  
  5.         g.Key.MonthSalary  
  6.     }).ToList();  
  7. Console.WriteLine("New EmployeeList with Salary");  
  8. foreach(var empsal in newEmployeeSalary) {  
  9.     Console.WriteLine("Salary : " + empsal.MonthSalary);  
  10. }  
Detailed Description
  • from t1 in empList -> we are assigning the emplist collection to t1
  • join t2 in salaryDetails on t1.Id equals t2.EmpID -> same salary details are in t2 on making join by comparing both table employee id
  • group t2 by new { t2.MonthSalary } -> here we are grouping the collection by monthsalary
  • into g-> once the grouping is done we put all grouped values in the container named g
  • select new { g.Key.MonthSalary }).ToList(); -> finally we are selecting the salarydetails from that container and representing this as list
Now, run this code and see the result. In the below result, we can see the 3 rows while we have 4 rows in the original list because the salary 80000 occurs twice and it's grouped as 1.

 

Uses of Let 

Let keyword allow us to store the result of the query which can be used in subsequent queries;  i.e., it creates a new variable and initializes it with the result of the expression you supply.

See the example:
  1. var letsal = (from e in salaryDetails  
  2.     let yourlogic = 60000  
  3.     where e.MonthSalary < yourlogic select e).ToList();  
  4. Console.WriteLine("New EmployeeList with Let Keyword");  
  5. foreach(var empsal in letsal) {  
  6.     Console.WriteLine("Emp Id:" + empsal.EmpID, "Emp Salary : " + empsal.MonthSalary);  
  7. }  
Detailed Description
  • from e in salaryDetails -> here we are binding all salary details in the name of e 
  • let yourlogic = 60000 -> here we can perform our own logic and here I mentioned static value as 60000 to that let variable
  • where e.MonthSalary < yourlogic -> now am filtering the salarydetails whichever < (greater than) yourlogic variable
Now, let's see the result to get more knowledge about this.

 

Now, let's see some logic statement with let variable.
  1. string[] mySkills = {  
  2.     "Asp.net",  
  3.     "MVC",  
  4.     "Linq",  
  5.     "Docusign",  
  6.     "EntityFramework",  
  7.     "Jquery",  
  8.     "C#",  
  9.     "SqlServer",  
  10.     "Orchard-CMS"  
  11. };  
  12. var skillandrank = from skills in mySkills  
  13. let skill = OurLogic(skills)  
  14. let rank = RankSkill(skill)  
  15. select new {  
  16.     skill,  
  17.     rank  
  18. };  
  19. Console.WriteLine("Let Keyword with Some Logics");  
  20. foreach(var item in skillandrank) {  
  21.     Console.WriteLine("Skill : " + item.skill + ", Rank : " + item.rank);  
  22. }  
  23. static string RankSkill(string value) {  
  24.     switch (value) {  
  25.         case "Asp.net":  
  26.         case "MVC":  
  27.         case "Linq":  
  28.             return "Top 3";  
  29.         case "Docusign":  
  30.         case "EntityFramework":  
  31.         case "Jquery":  
  32.             return "Top 6";  
  33.         case "C#":  
  34.         case "SqlServer":  
  35.         case "Orchard-CMS":  
  36.             return "Top 10";  
  37.         default:  
  38.             return "Not in Rank";  
  39.     }  
  40. }  
  41. static string OurLogic(string value) {  
  42.     switch (value) {  
  43.         case "Asp.net":  
  44.         case "MVC":  
  45.         case "Linq":  
  46.         case "Docusign":  
  47.         case "EntityFramework":  
  48.         case "Jquery":  
  49.         case "C#":  
  50.         case "SqlServer":  
  51.         case "Orchard-CMS":  
  52.             return value;  
  53.         default:  
  54.             return "Other's";  
  55.     }  
  56. }  
Detailed Description 
  • from skills in mySkills->  we are calling all skills in the name of skill variable
  • let skill = OurLogic(skills) -> each time it's executed for each skill it will return the skill name and it's stored in skill variable(let)
  • let rank = RankSkill(skill) -> this is also executed for each skill and it will return the rank for each skill and it's stored in rank variable(let)
  • select new { skill, rank }; -> finally the let variable values are selected by using select new.
Okay, now we will see the result to get clear picture of this concept.

 

Now, let's get into more details about these two.

Differences between Into and Let keyword

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

 Let Into
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.
 
ex:
var skillandrank = from skills in mySkills
let skill = OurLogic(skills)
let rank = RankSkill(skill)
select new { skill, rank };
 
 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
.
ex:
var newEmployeeList = from e in empList
group e by new { e.YOE, e.Designation }
into gEmployee
where gEmployee.Count() > 1
select new { gEmployee.Key.YOE, gEmployee.Key.Designation, totalemplcount = gEmployee.Count() };
 
Like var, a let variable has an implicit type. We can resuse the variable in subsequent statement(query) so that we can avoid more dublicate work as well as we can easily handle the complex query It holds the value in temporary variable as collection but not in subsequent statement(query), it will make us write dublicate query.
 
Summary

In this article, we learned how to use the Let and Into keywords in LINQ query. I hopw now you have a clear picture of both the keywords and their differences.

Let 


This keyword is a part of a query expression. It introduces a variable. We can, then, reuse the variable elsewhere in the query. 
 
Into

The Into contextual keyword can be used to create a temporary identifier to store the results of a group, join or select clause into a new identifier. This identifier can itself be a generator for additional query commands.

I hope you enjoyed this article and your valuable feedback is always welcome. 


Similar Articles