Mastering the Core Operations That Power LINQ Queries
LINQ (Language Integrated Query) revolutionised how C# developers interact with data.
Instead of juggling loops, lists, and conditionals, you can describe what you want – and let LINQ handle the how.
In this tutorial, we’ll explore three of LINQ’s most essential operations — Filtering, Projecting, and Ordering — and how they work together to turn raw data into structured, meaningful results.
🔹 LINQ Syntax Overview
LINQ brings SQL-style querying directly into C#:
var results = from item in collection
where item.Price > 50
orderby item.Name
select item.Name;
This declarative style is both readable and powerful — and works on arrays, lists, XML, databases, and more.
🔹 Filtering Data – Using where
Filtering is about narrowing your data set down to only what’s relevant.
The where clause acts like a filter condition, just like WHERE in SQL.
Example:
var numbers = new[] { 5, 10, 15, 20, 25, 30 };
var filtered = from n in numbers
where n > 10 && n < 30
select n;
Output:
15, 20, 25
✅ Best for:
- Removing unwanted data
- Combining multiple logical expressions (
&&,||,!) - Filtering based on string, numeric, or object criteria
🔧 Tip:
You can use methods inside where:
where name.StartsWith("A") || name.EndsWith("s")
🔹 Projecting Data – Using select
Projection means transforming your data into a new form — selecting only the parts you need, or creating entirely new objects.
Example 1 – Selecting specific fields
var students = new[]
{
new { Name = "Alice", Age = 22 },
new { Name = "Bob", Age = 25 },
new { Name = "Clara", Age = 20 }
};
var names = from s in students
select s.Name;
Output:
Alice, Bob, Clara
Example 2 – Creating new anonymous types
var projections = from s in students
select new { StudentName = s.Name, NextYearAge = s.Age + 1 };
Output:
StudentName = Alice, NextYearAge = 23
StudentName = Bob, NextYearAge = 26
StudentName = Clara, NextYearAge = 21
✅ Best for:
- Extracting key information
- Reshaping data for output or display
- Building lightweight view models
🔧 Tip:
Use select new { ... } to construct flexible, temporary types for reports and UI binding.
🔹 Ordering Data – Using orderby
Sorting helps you present or process data in a specific sequence.
By default, orderby sorts ascending, but you can easily specify descending.
Example 1 – Simple ascending sort
var fruits = new[] { "Orange", "Apple", "Mango", "Banana" };
var sorted = from f in fruits
orderby f
select f;
Output:
Apple, Banana, Mango, Orange
Example 2 – Descending and multi-level sort
var employees = new[]
{
new { Name = "John", Department = "Sales", Salary = 50000 },
new { Name = "Anna", Department = "Sales", Salary = 55000 },
new { Name = "Mark", Department = "IT", Salary = 60000 },
new { Name = "Lucy", Department = "IT", Salary = 58000 }
};
var sortedEmps =
from e in employees
orderby e.Department, e.Salary descending
select e;
Output:
Department: IT → Mark (60000), Lucy (58000)
Department: Sales → Anna (55000), John (50000)
✅ Best for:
- Displaying results in user-friendly order
- Sorting by multiple fields
- Combining with filtering for precise control
🔧 Tip:
To reverse the order later, use .Reverse() after the query.
🧩 Combined Example – Filter → Project → Order
Let’s combine everything into one practical LINQ query.
var products = new[]
{
new { Name = "Laptop", Price = 1200 },
new { Name = "Mouse", Price = 25 },
new { Name = "Keyboard", Price = 45 },
new { Name = "Monitor", Price = 200 }
};
var query =
from p in products
where p.Price > 30
orderby p.Price descending
select new
{
Product = p.Name,
Discounted = p.Price * 0.9
};
Output:
Product = Laptop, Discounted = 1080
Product = Monitor, Discounted = 180
Product = Keyboard, Discounted = 40.5
✅ What’s Happening:
wherefilters out cheap productsorderbysorts by price, highest firstselectprojects a new result showing a discounted value
📚 Summary
| Concept | Keyword | Description | Example |
|---|---|---|---|
| Filtering | where | Select only the elements matching a condition | where price > 50 |
| Projecting | select | Shape or transform the result | select new { Name, Price } |
| Ordering | orderby | Sort ascending or descending | orderby score descending |
✅ Best Practices
- Keep queries declarative and readable — avoid deeply nested logic
- Filter early using
whereto reduce data before sorting - Always specify
descendingexplicitly when needed - Use
selectto create lightweight, anonymous result sets - Chain filters and sorts logically: Filter → Order → Project
🧪 Challenge Task
Write a LINQ query for a list of employees that:
- Filters out anyone earning below £40,000
- Orders the results by department (ascending) and salary (descending)
- Projects a new object with
{ Name, Department, AnnualBonus = Salary * 0.1 }
Example:
var staff = new[]
{
new { Name = "Tom", Department = "IT", Salary = 60000 },
new { Name = "Emma", Department = "HR", Salary = 35000 },
new { Name = "Sam", Department = "Finance", Salary = 50000 }
};
👨💻 Want More?
Our Advanced C# Course dives deeper into:
- Filtering, ordering, grouping, and joining data
- Working with LINQ to Objects and LINQ to SQL
- Real-world scenarios using collections and databases
- Optimizing query performance and clarity
Master these LINQ fundamentals and you’ll write data-handling code that’s clean, fast, and professional.