Use Let to create new value and use it later in LINQ in CSharp

Description

The following code shows how to use Let to create new value and use it later in LINQ.

Example


     /*  www. j av a2  s  . c  o m*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


public class MainClass{

   public static void Main(string[] args){   
            int[] ArrayA = { 1, 2, 3, 4 };
            int[] ArrayB = { 1, 2, 3, 4 };

            var Squares = from QueryA in ArrayA
                from QueryB in ArrayB
                let TheSquare = QueryA * QueryB
                where TheSquare > 4
                select new { QueryA, QueryB, TheSquare };

            foreach (var ThisSquare in Squares)
                Console.WriteLine(ThisSquare.QueryA.ToString() + " * " + 
                   ThisSquare.QueryB.ToString() + " = " + 
                   ThisSquare.TheSquare.ToString());


   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ