C# LINQ let
last modified November 15, 2026
This article shows how to use the let clause in LINQ query
expressions to store intermediate results, and explains what the compiler
does with it.
The let clause introduces a new range variable and initializes it
with the result of an expression. The variable is computed once for each
element of the source and can be used in all later clauses of the query:
where, orderby, group, and
select. Like other range variables, it is read-only.
A let variable saves us from repeating an expression, so the
query is shorter, and the value is not computed several times. It also gives
a meaningful name to a partial result.
Basic let example
The simplest use of let stores a value that is needed in more
than one clause.
string[] words = ["apple", "banana", "cherry", "date", "elderberry"];
var query = from word in words
let length = word.Length
where length > 5
select new { Word = word, Length = length };
foreach (var item in query)
{
Console.WriteLine($"{item.Word} - {item.Length} letters");
}
We compute the length of each word once and use it in the filter and in the projection.
let length = word.Length
The let clause creates the length range variable,
which is available in the rest of the query.
$ dotnet run banana - 6 letters cherry - 6 letters elderberry - 10 letters
let with computed values
let is most useful for values that need a calculation.
List<Product> products =
[
new("Laptop", 999.99m, 5),
new("Phone", 699.99m, 10),
new("Tablet", 349.99m, 3),
new("Monitor", 249.99m, 7)
];
var query = from p in products
let totalValue = p.Price * p.Stock
where totalValue > 2000
orderby totalValue descending
select new { p.Name, TotalValue = totalValue };
foreach (var product in query)
{
Console.WriteLine($"{product.Name}: {product.TotalValue:F2}");
}
record Product(string Name, decimal Price, int Stock);
We compute the total inventory value of each product and use it for filtering, sorting, and in the result.
let totalValue = p.Price * p.Stock where totalValue > 2000 orderby totalValue descending
The multiplication is written once. Only the phone (6999.90) and the laptop (4999.95) are worth more than 2000; the tablet and the monitor are filtered out.
$ dotnet run Phone: 6999.90 Laptop: 4999.95
Multiple let clauses
A query can have several let clauses. Each of them can use the
variables introduced before it.
List<string> names =
[
"John Smith",
"Alice Johnson",
"Robert Brown",
"Emily Davis",
"Michael Wilson"
];
var query = from name in names
let parts = name.Split(' ')
let firstName = parts[0]
let lastName = parts[1]
let initials = $"{firstName[0]}{lastName[0]}"
orderby lastName
select new { FullName = name, Initials = initials };
foreach (var person in query)
{
Console.WriteLine($"{person.Initials}: {person.FullName}");
}
We split full names into parts, build initials, and sort by the last name.
let parts = name.Split(' ')
let firstName = parts[0]
let lastName = parts[1]
let initials = $"{firstName[0]}{lastName[0]}"
The Split method is called once per name. The following clauses
build on its result, step by step.
orderby lastName
A let variable can be used as a sort key, although it is not part
of the result.
$ dotnet run RB: Robert Brown ED: Emily Davis AJ: Alice Johnson JS: John Smith MW: Michael Wilson
let with method calls
A let clause can store the result of a method call, so the
method is called once per element.
List<DateTime> dates =
[
new(2026, 1, 15),
new(2026, 3, 22),
new(2026, 6, 8),
new(2026, 9, 30),
new(2026, 11, 5),
new(2026, 12, 24)
];
int calls = 0;
var query = from date in dates
let quarter = GetQuarter(date)
where quarter > 2
group date by quarter into g
select new { Quarter = g.Key, Dates = g };
foreach (var group in query)
{
Console.WriteLine($"Q{group.Quarter}:");
foreach (var date in group.Dates)
{
Console.WriteLine($" {date:yyyy-MM-dd}");
}
}
Console.WriteLine($"GetQuarter calls: {calls}");
int GetQuarter(DateTime date)
{
calls++;
return (date.Month - 1) / 3 + 1;
}
We determine the quarter of each date, keep the second half of the year, and group the dates by quarter.
let quarter = GetQuarter(date) where quarter > 2 group date by quarter into g
The quarter is used both by the filter and as the group key.
Console.WriteLine($"GetQuarter calls: {calls}");
The counter shows six calls, one per date, even though the quarter is used twice.
$ dotnet run Q3: 2026-09-30 Q4: 2026-11-05 2026-12-24 GetQuarter calls: 6
How let is translated
Query syntax is translated to method calls. A let clause becomes
a Select that pairs each element with the new value in an
anonymous type, called a transparent identifier. The later clauses
read the values from this pair.
string[] words = ["apple", "banana", "cherry", "date", "elderberry"];
var q1 = from word in words
let length = word.Length
where length > 5
select $"{word}:{length}";
var q2 = words
.Select(word => new { word, length = word.Length })
.Where(t => t.length > 5)
.Select(t => $"{t.word}:{t.length}");
Console.WriteLine(string.Join(", ", q1));
Console.WriteLine(string.Join(", ", q2));
We write the same query in query syntax and in method syntax.
var q2 = words
.Select(word => new { word, length = word.Length })
.Where(t => t.length > 5)
.Select(t => $"{t.word}:{t.length}");
This is roughly what the compiler generates for q1. In method
syntax, we would usually skip the intermediate type and call
word.Length twice, or use a statement lambda.
$ dotnet run banana:6, cherry:6, elderberry:10 banana:6, cherry:6, elderberry:10
let versus another from clause
An additional from clause iterates over an inner collection and
flattens the results. A let clause keeps the inner result as a
single value for each element.
List<List<int>> numberLists =
[
[1, 2, 3],
[4, 5, 6],
[7, 9, 11]
];
var flat = from list in numberLists
from number in list
where number % 2 == 0
select number;
Console.WriteLine($"from: {string.Join(", ", flat)}");
var nested = from list in numberLists
let evens = list.Where(n => n % 2 == 0).ToList()
select new { List = list, Evens = evens };
foreach (var item in nested)
{
Console.WriteLine($"let: [{string.Join(", ", item.List)}] -> [{string.Join(", ", item.Evens)}]");
}
We find the even numbers in a list of lists.
var flat = from list in numberLists
from number in list
Two from clauses produce a single flat sequence of numbers.
let evens = list.Where(n => n % 2 == 0).ToList()
With let, each inner list stays together with its own even
numbers. The last list has none, but it still appears in the result.
$ dotnet run from: 2, 4, 6 let: [1, 2, 3] -> [2] let: [4, 5, 6] -> [4, 6] let: [7, 9, 11] -> []
let stores queries, not results
When the expression in a let clause is itself a deferred LINQ
query, the variable holds the query. It is executed again each time it is
enumerated.
var students = new[]
{
new { Name = "Anna", Grades = new[] { 90, 85, 92 } },
new { Name = "Ben", Grades = new[] { 78, 81, 86 } },
new { Name = "Cara", Grades = new[] { 88, 94, 91 } }
};
int checks = 0;
var lazy = from s in students
let high = s.Grades.Where(g => { checks++; return g >= 90; })
where high.Any()
select new { s.Name, High = high };
foreach (var s in lazy)
{
Console.WriteLine($"{s.Name}: {string.Join(", ", s.High)}");
}
Console.WriteLine($"predicate calls: {checks}");
checks = 0;
var eager = from s in students
let high = s.Grades.Where(g => { checks++; return g >= 90; }).ToArray()
where high.Length > 0
select new { s.Name, High = high };
foreach (var s in eager)
{
Console.WriteLine($"{s.Name}: {string.Join(", ", s.High)}");
}
Console.WriteLine($"predicate calls: {checks}");
We find students with at least one grade of 90 or more, and count how often the predicate runs.
let high = s.Grades.Where(g => { checks++; return g >= 90; })
where high.Any()
high is an unexecuted query. Any runs it once, and
string.Join runs it again for each selected student: twelve
predicate calls in total.
let high = s.Grades.Where(g => { checks++; return g >= 90; }).ToArray()
where high.Length > 0
With ToArray, the grades of each student are filtered once, nine
calls for nine grades. The array is then used by the where
clause and printed.
$ dotnet run Anna: 90, 92 Cara: 94, 91 predicate calls: 12 Anna: 90, 92 Cara: 94, 91 predicate calls: 9
Source
Query expression translation - C# specification
In this article we have used the LINQ let clause to store
intermediate values in query expressions. See also the
LINQ query expression,
LINQ deferred execution, and
LINQ group by tutorials.
Author
List all C# tutorials.