Understanding the let Keyword in LINQ

Morteza JangjooMorteza Jangjoo
3 min read

When writing LINQ queries in C#, you might have come across the let keyword. At first glance, it may look a bit mysterious — but once you get the hang of it, you’ll see that let is a simple yet powerful feature that makes your queries more readable, maintainable, and efficient.


What is let in LINQ?

In LINQ query syntax, let allows you to introduce a new range variable inside your query.
Think of it as a way to store the result of a calculation, transformation, or intermediate value so you can reuse it later in the same query.

Without let, you would often need to repeat the same expression multiple times.


Syntax

from item in collection
let variable = someCalculation(item)
where variable > 10
select new { item, variable };

A Simple Example

Let’s say we want to filter words by their length and also project the word along with its length:

string[] words = { "one", "two", "three", "four", "five" };

var query =
    from w in words
    let length = w.Length
    where length > 3
    select new { Word = w, Length = length };

foreach (var item in query)
    Console.WriteLine($"{item.Word} - {item.Length}");

Output:

three - 5
four - 4
five - 4

Here, let prevents us from repeating w.Length and makes the query much cleaner.


Another Example: Palindromes

We can also use let to store transformations, such as reversing a string:

string[] words = { "level", "world", "civic", "radar", "test" };

var query =
    from w in words
    let reversed = new string(w.Reverse().ToArray())
    where w == reversed
    select w;

foreach (var item in query)
    Console.WriteLine(item);

Output:

level
civic
radar

Why Use let?

  1. Avoid Repetition – Perform a calculation once and reuse it.

  2. Improve Readability – Break complex expressions into smaller parts.

  3. Maintainability – Easier to modify later, since logic is centralized.


let vs into

A common confusion is between let and into.

  • let defines a temporary variable inside the query.

  • into creates a new query continuation after operations like group or join.

Example with into:

var query =
    from w in words
    group w by w[0] into g
    where g.Count() > 1
    select new { Letter = g.Key, Count = g.Count() };

Here into lets us continue querying the grouped data.


Conclusion

The let keyword in LINQ is a small but powerful feature that improves clarity and reduces duplication in queries.
Whenever you find yourself repeating the same expression inside a LINQ query, consider introducing a let variable. It makes your code both cleaner and more efficient.


👉 Pro tip: Start using let in your everyday LINQ queries — you’ll notice how much more expressive your code becomes.


I’m Morteza Jangjoo and “Explaining things I wish someone had explained to me”

0
Subscribe to my newsletter

Read articles from Morteza Jangjoo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Morteza Jangjoo
Morteza Jangjoo