有时,当我们使用查询表达式时,我们需要一个变量,该变量可以存储子表达式的结果,以便在即将出现的子句中重新使用它。这种类型的功能由Let关键字提供。使用Let关键字,您可以创建范围变量并使用查询表达式的结果进行初始化,然后可以在同一查询中将该变量与即将出现的子句一起使用。当您之后使用一个值初始化范围变量时,则不允许在范围变量中存储另一个值。
范例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);
}
}
}
输出:
178
190
说明:在上面的示例中,结果是查询变量,该变量存储给定查询表达式的结果。在查询表达式中,我们使用let关键字a1创建了一个范围变量。在a1中,我们存储s + 100表达式的结果。之后,我们在where子句中使用此变量比较每个元素是否大于150,最后打印大于150的那些元素。
示例: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 emp = new List() {
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);
}
}
}
输出:
Employee Name: rohit
Employee Name: supriya