Open In App

LINQ | Let Keyword

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Sometimes when we are working with a query expression, we require a variable that can store the result of the sub-expression in order to reuse it in the upcoming clause. This type of facility is provided by the Let keyword. The Let keyword allows you to create a range variable and initialized with the result of the query expression and then you are allowed to use that variable with the upcoming clause in the same query. When you initialize a range variable with one value after that you are not allowed to store another value in the range variable.

Example 1:




// C# program to illustrate the
// concept of the let keyword
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Data source
        int[] sequence = {2, 78, 90,
                          5, 45, 6};
  
        // Using let keyword
        var result = from s in sequence
            let a1
            = s + 100 where a1 > 150 select a1;
  
        // Display the result
        foreach(var val in result)
        {
            Console.WriteLine(val);
        }
    }
}


Output:

178
190

Explanation: In the above example, the result is the query variable which stores the result of the given query expression. In the query expression, we created a range variable using let keyword, i.e, a1. In a1, we store the result of the s + 100 expressions. After that we use this variable in where clause to compare each element is greater than 150 or not and last we print those elements which are greater than 150.

Example: 2




// C# program to print the name of those
// employees whose name contain more
// than 4 characters in lowercase
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "ANU", emp_gender = "Female",
                                 emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "SIYA", emp_gender = "Female",
                                  emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "ROHIT", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
                              
            new Employee() {emp_id = 212, emp_name = "SUPRIYA", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "ANIL", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "ANJU", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to print the name of those
        // employees whose name contain more
        // than 4 characters in lowercase
        // Using let clause
        var res = from e in emp
            let name
            = e.emp_name.ToLower()
                where name.Length
            > 4 select name;
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0}", val);
        }
    }
}


Output:

Employee Name: rohit
Employee Name: supriya


Last Updated : 29 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads