LINQ Keywords: let
The let keyword is used to create a local variable in the LINQ statement. Here is an example:
1 min read30 words2 code samples
Description#
The let keyword is used to create a local variable in the LINQ statement. The following example shows this:
C#
using System;
using System.Linq;
public class Employee
{
public int EmployeeNumber;
public string FirstName;
public string LastName;
public decimal HourlySalary;
public Employee(int number = 0,
string firstName = "John",
string lastName = "Doe",
decimal salary = 0M)
{
EmployeeNumber = number;
FirstName = firstName;
LastName = lastName;
HourlySalary = salary;
}
}
public class Exercise
{
public static int Main()
{
var employees = new Employee[]
{
new Employee(971974, "Patricia", "Katts", 24.68M),
new Employee(208411, "Raymond", "Kouma", 20.15M),
new Employee(279374, "Hélène", "Mukoko", 15.55M),
new Employee(707912, "Bertrand", "Yamaguchi", 24.68M),
new Employee(971394, "Gertrude", "Monay", 20.55M)
};
var fullNames = from empls
in employees
let FullName = empls.LastName + ", " + empls.FirstName
select FullName;
Console.WriteLine("+=====================+");
Console.WriteLine(" Full Names ");
Console.WriteLine("----------------------");
foreach (var staff in fullNames)
Console.WriteLine(" {0}", staff);
Console.WriteLine("+=====================+");
Console.WriteLine();
return 0;
}
}The output would be:
C#
+=====================+
Full Names
----------------------
Katts, Patricia
Kouma, Raymond
Mukoko, Hélène
Yamaguchi, Bertrand
Monay, Gertrude
+=====================+
Press any key to continue . . .Home Copyright © 2008-2010 FunctionX, Inc.

