Mastering LINQ’s Core Query Keywords
LINQ (Language Integrated Query) is one of C#’s most powerful features for working with data collections.
It brings SQL-like syntax right into your code, allowing you to query, transform, and filter data using clean, expressive statements.
In this tutorial, we’ll explore four key LINQ query operators — let, where, orderby, and select and how they work together to make data manipulation both elegant and efficient.
🔹 The Building Blocks of a LINQ Query
A typical LINQ query looks like this:
var results = from item in collection
where item.Price > 50
orderby item.Name
select item.Name;
Each keyword has a specific role:
| Keyword | Purpose |
|---|---|
| let | Creates a temporary variable within a query |
| where | Filters data based on a condition |
| orderby | Sorts the result set |
| select | Defines the output or projection |
🔹 let – Create Variables Inside Your Query
The let keyword allows you to store the result of a calculation or expression in a new variable that you can reuse later in the same query.
It’s especially useful when working with derived values or when you want to simplify complex conditions.
Example:
var results = from num in Enumerable.Range(1, 10)
let square = num * num
where square > 20
select new { Number = num, Square = square };
Output:
Number = 5, Square = 25
Number = 6, Square = 36
...
Number = 10, Square = 100
✅ Best for:
- Simplifying expressions
- Improving readability
- Avoiding repeated calculations
🔧 Tip: Use let to make queries more expressive — not to hold large temporary data unnecessarily.
🔹 where – Filter the Data
The where clause filters a sequence to only include elements that satisfy a condition.
It works similarly to the WHERE keyword in SQL.
Example:
var numbers = new[] { 10, 25, 40, 55, 70 };
var result = from n in numbers
where n > 30 && n < 60
select n;
Output:
40, 55
✅ Best for:
- Filtering based on numeric, string, or object conditions
- Combining multiple logical expressions
🔧 Key Rules:
- Returns elements that evaluate to true for the condition
- Can be chained multiple times for layered filtering
🔹 orderby – Sort the Results
The orderby clause sorts the query output in ascending order by default, but you can specify descending using the descending keyword.
Example:
var fruits = new[] { "Banana", "Apple", "Cherry", "Mango" };
var sorted = from f in fruits
orderby f
select f;
Output:
Apple, Banana, Cherry, Mango
Now let’s sort descending:
var sortedDesc = from f in fruits
orderby f descending
select f;
Output:
Mango, Cherry, Banana, Apple
✅ Best for:
- Alphabetical sorting
- Sorting by numeric or date values
🔧 Advanced Tip: You can order by multiple keys:
orderby student.Grade descending, student.Name ascending
🔹 select – Choose What to Return
The select keyword defines what your query returns.
It can project entire objects, specific fields, or even anonymous types.
Example 1 – Selecting entire objects
var result = from s in students
select s;
Example 2 – Selecting specific properties
var names = from s in students
select s.Name;
Example 3 – Creating a new anonymous type
var summary = from s in students
select new { s.Name, s.Age };
✅ Best for:
- Shaping your data output
- Returning only what’s needed
- Creating custom projections on the fly
🔧 Tip: Always select only what you need — it improves readability and performance.
🧩 Combined Example – Using All Four Together
var students = new[]
{
new { Name = "Alice", Score = 85 },
new { Name = "Bob", Score = 67 },
new { Name = "Charlie", Score = 90 },
new { Name = "Diana", Score = 72 }
};
var topStudents =
from s in students
let grade = s.Score >= 80 ? "A" : "B"
where s.Score > 70
orderby s.Score descending
select new { s.Name, s.Score, Grade = grade };
Output:
Name: Charlie, Score: 90, Grade: A
Name: Alice, Score: 85, Grade: A
Name: Diana, Score: 72, Grade: B
✅ What’s Happening:
letcreates a grade label based on the scorewherefilters out low scoresorderbysorts highest to lowestselectshapes the final output
📚 Summary
| Keyword | Description | Example |
|---|---|---|
| let | Creates a temporary variable | let total = price * qty |
| where | Filters based on condition | where age > 18 |
| orderby | Sorts ascending or descending | orderby name descending |
| select | Defines the shape of the output | select new { Name, Age } |
✅ Best Practices
- Use
letto clarify logic, not complicate it - Keep
whereclauses simple and readable - Always specify
descendingwhen needed for clarity - Use
selectto project only necessary data - Combine these operators for elegant, SQL-like queries
🧪 Challenge Task
Write a LINQ query to process a list of products:
- Use
letto calculate discounted price - Use
whereto filter items under £50 - Use
orderbyto sort cheapest first - Use
selectto return{ Name, DiscountedPrice }
Example:
var products = new[]
{
new { Name = "Shoes", Price = 60 },
new { Name = "Hat", Price = 30 },
new { Name = "Coat", Price = 90 },
new { Name = "Scarf", Price = 20 }
};
👨💻 Want More?
Contact James to learn the depths of LINQ:
let,where,orderby, andselect- Grouping and joining data
- Lambda expressions and query syntax
- Real-world data manipulation projects
Master LINQ and make your C# data queries cleaner, faster, and smarter.