Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code equivalent to the 'let' keyword in chained LINQ extension method calls

People also ask

What is let keyword in LINQ?

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.

What are extension methods in LINQ?

Extension Methods are a new feature in C# 3.0, and they're simply user-made pre-defined functions. An Extension Method enables us to add methods to existing types without creating a new derived type, recompiling, or modifying the original types.

Does C# have let?

C# let KeywordUse the let keyword in query expressions to declare and assign variables that can be reused. Let. This C# keyword is a part of a query expression. It introduces a variable.

What is let variable in C#?

The 'let' keyword is useful in query syntax. It projects a new range variable, allows re-use of the expression and makes the query more readable. For example, you can compare string values and select the lowercase string value as shown below: Example: let in LINQ query - C#


Let doesn't have its own operation; it piggy-backs off of Select. You can see this if you use "reflector" to pull apart an existing dll.

it will be something like:

var result = names
        .Select(animalName => new { nameLength = animalName.Length, animalName})
        .Where(x=>x.nameLength > 3)
        .OrderBy(x=>x.nameLength)
        .Select(x=>x.animalName);

There's a good article here

Essentially let creates an anonymous tuple. It's equivalent to:

var result = names.Select(
  animal => new { animal = animal, nameLength = animal.Length })
.Where(x => x.nameLength > 3)
.OrderBy(y => y.nameLength)
.Select(z => z.animal);

There is also a .Let extension method in System.Interactive, but its purpose is to introduce a lambda expression to be evaluated 'in-line' in a fluent expression. For instance, consider (in LinqPad, say) the following expression that creates new random numbers every time it's executed:

var seq = EnumerableEx.Generate(
    new Random(),
    _ => true,
    _ => _,
    x => x.Next());

To see that new random samples show up every time, consider the following

seq.Zip(seq, Tuple.Create).Take(3).Dump();

which produces pairs in which the left and right are different. To produce pairs in which the left and right are always the same, do something like the following:

seq.Take(3).ToList().Let(xs => xs.Zip(xs, Tuple.Create)).Dump(); 

If we could invoke lambda expressions directly, we might write

(xs => xs.Zip(xs, Tuple.Create))(seq.Take(3).ToList()).Dump();

But we can't invoke lambda expressions as if they were methods.


about Code equivalent to the 'let' keyword in chained LINQ extension method calls

above comment is no more valid

var x = new List<int> { 2, 3, 4, 5, 6 }.AsQueryable();
(from val in x
let val1 = val
let val2 = val + 1
where val2 > val1
select val
).Dump();

produces

System.Collections.Generic.List`1[System.Int32]
.Select(
  val =>
     new
     {
         val = val,
         val1 = val
     }
)
.Select(
  temp0 =>
     new
     {
         temp0 = temp0,
         val2 = (temp0.val + 1)
     }
)
.Where(temp1 => (temp1.val2 > temp1.temp0.val1))
.Select(temp1 => temp1.temp0.val)

so multiple let are optimized now