LINQ Keywords: let

LINQ Keywords: let



The let keyword is used to create a local variable in the LINQ statement. Here is an example:

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(" Full Names            ");
        foreach (var staff in fullNames)
            Console.WriteLine(" {0}", staff);


        return 0;

This would produce:

 Full Names
 Katts, Patricia
 Kouma, Raymond
 Mukoko, Hél�ne
 Yamaguchi, Bertrand
 Monay, Gertrude

Press any key to continue . . .

Home Copyright © 2008-2015, FunctionX, Inc.