Filter and Project Products
Create a LINQ pipeline over product records.
Introduction
A query can filter, order, and project without modifying its source collection.
Anonymous objects are useful for short-lived projections that do not need a named type.
Runnable C# example
Program.cs
using System;
using System.Linq;
record Product(string Name, decimal Price);
class Program
{
static void Main()
{
var products = new[] { new Product("Book", 30), new Product("Pen", 5), new Product("Bag", 45) };
var names = products.Where(p => p.Price >= 20).OrderBy(p => p.Price).Select(p => p.Name);
Console.WriteLine(string.Join(", ", names));
}
}
Book, BagPractice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Sort by a Chosen Key Sort people by name or by age with a comparer chosen at run time, breaking ties consistently. Easy
- Above-Average Values Keep the values that are greater than the average. Easy