C# LINQ basics: Where, Select and OrderBy
October 30, 2026 · 2 min read
Use Where to keep items that pass a test, Select to change each item into something else, and OrderBy to sort. Chain them as Where, then OrderBy, then Select, and finish with ToList().
Filter, sort and project a menu
List<MenuItem> menu =
[
new MenuItem("Espresso", 3.25m), new MenuItem("Latte", 4.75m),
new MenuItem("Cold brew", 4.25m), new MenuItem("Drip coffee", 2.75m),
new MenuItem("Blueberry muffin", 3.50m),
];
var result = menu.Where(item => item.Price < 4.50m)
.OrderBy(item => item.Price)
.Select(item => $"{item.Name} ({item.Price:0.00})")
.ToList();
foreach (var line in result)
{
Console.WriteLine(line);
}
record MenuItem(string Name, decimal Price);
Output:
Drip coffee (2.75) Espresso (3.25) Blueberry muffin (3.50) Cold brew (4.25)
How it works
LINQ is a set of methods that work on any list or array. In a normal dotnet project the using System.Linq; line is already added for you.
item => item.Price < 4.50mis a lambda: a tiny method written in place. The name on the left of=>is the parameter (you pick it), and the value on the right is what it returns.Wherecalls the lambda once per item and keeps the ones that givetrue. The Latte costs 4.75, so it is dropped.OrderBysorts by the value the lambda returns, smallest first. UseOrderByDescendingfor largest first, andThenByto break ties.Selectturns each item into something new. Here eachMenuItembecomes a string.ToList()finishes the chain and gives you a realList. UseToArray()if you want an array.
None of these methods change menu. They build a new result each time.
Common mistake
Putting Select before Where or OrderBy and then asking for a property that no longer exists:
// Error: a string has no Price
var bad = menu.Select(item => item.Name).Where(name => name.Price < 4m);
After Select, you are working with whatever it returned. Filter and sort first, then project:
var good = menu.Where(item => item.Price < 4m).Select(item => item.Name).ToList();
Also remember the final ToList(). Without it you have a sequence, not a list, so .Count and indexing like result[0] will not work.
Takeaway
Read a LINQ chain top to bottom: filter with Where, sort with OrderBy, reshape with Select, then call ToList().