Select and Projection
Shape data into new forms.
Select and Projection is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Projection Means
Select transforms each element of a sequence into a new shape. This is called projection.
It takes a lambda that maps one element to a result, and returns a sequence of those results — leaving the source untouched.
using System;
using System.Linq;
int[] nums = { 1, 2, 3, 4 };
var squares = nums.Select(n => n * n);
Console.WriteLine(string.Join(", ", squares));Changing the Type
Projection can change the element type entirely. Mapping int to string produces an IEnumerable<string>.
The output type is inferred from the lambda's return type, so the compiler tracks it for you.
using System;
using System.Linq;
int[] nums = { 10, 20, 30 };
var labels = nums.Select(n => $"Item #{n}");
foreach (var l in labels) Console.WriteLine(l);Projecting Properties
A frequent use is pulling a single property out of objects. Select(p => p.Name) turns a list of objects into a list of names.
This narrows data to just what you need before further processing or display.
using System;
using System.Linq;
using System.Collections.Generic;
record User(string Name, int Age);
var users = new List<User> { new("Ana", 30), new("Bo", 25) };
var names = users.Select(u => u.Name);
Console.WriteLine(string.Join(", ", names));Anonymous Type Results
You can project into an anonymous type to keep several fields together. Use new { ... } inside the lambda.
The compiler generates a class on the fly with read-only properties, ideal for shaping intermediate results.
using System;
using System.Linq;
using System.Collections.Generic;
record User(string Name, int Age);
var users = new List<User> { new("Ana", 30), new("Bo", 25) };
var shaped = users.Select(u => new { u.Name, IsAdult = u.Age >= 18 });
foreach (var s in shaped) Console.WriteLine($"{s.Name}:{s.IsAdult}");Select with Index
Like Where, Select has an overload exposing the zero-based index. The lambda takes (element, index).
This is perfect for numbering items or pairing each value with its position.
using System;
using System.Linq;
string[] tasks = { "Wake", "Eat", "Code" };
var numbered = tasks.Select((t, i) => $"{i + 1}. {t}");
foreach (var n in numbered) Console.WriteLine(n);Query Syntax Select
In query syntax, the select clause does projection. You can select the element itself or any expression based on it.
Combine it with where to filter and shape in one readable query.
using System;
using System.Linq;
int[] nums = { 1, 2, 3, 4, 5 };
var result = from n in nums
where n % 2 == 1
select n * 10;
Console.WriteLine(string.Join(", ", result));SelectMany Flattens
When each element maps to a collection, Select would give you a sequence of sequences. SelectMany flattens those into one sequence.
It is the LINQ equivalent of a nested loop that concatenates inner results.
using System;
using System.Linq;
using System.Collections.Generic;
var groups = new List<int[]> { new[] {1, 2}, new[] {3, 4} };
var flat = groups.SelectMany(g => g);
Console.WriteLine(string.Join(", ", flat));Projection Is Deferred Too
Select shares the deferred execution model. The mapping lambda runs only when the sequence is enumerated.
So building a projection is cheap; the work happens when you iterate or materialize the result.
using System;
using System.Linq;
int runs = 0;
int[] nums = { 1, 2, 3 };
var q = nums.Select(n => { runs++; return n; });
Console.WriteLine($"Before: {runs}");
foreach (var _ in q) { }
Console.WriteLine($"After: {runs}");Filter Then Project
Order matters for clarity and efficiency: filter first with Where, then project with Select, so you only transform elements you keep.
This pipeline reads top-to-bottom and avoids wasted projection work.
using System;
using System.Linq;
int[] nums = { 1, 2, 3, 4, 5, 6 };
var result = nums.Where(n => n % 2 == 0)
.Select(n => n * n);
Console.WriteLine(string.Join(", ", result));Projecting to a New Record
Instead of anonymous types you can project into a named type or record. This is useful when results cross method boundaries, since anonymous types cannot be returned with a known name.
It is also how DTOs are built from richer domain objects.
using System;
using System.Linq;
using System.Collections.Generic;
record User(string Name, int Age);
record Summary(string Name);
var users = new List<User> { new("Ana", 30) };
var dtos = users.Select(u => new Summary(u.Name));
foreach (var d in dtos) Console.WriteLine(d.Name);Select Keeps Order and Count
Unlike Where, Select never adds or removes elements — it maps one-to-one. The output has the same count and order as the input.
If you need fewer items, filter; if you need more from each, use SelectMany.
using System;
using System.Linq;
int[] nums = { 5, 1, 9 };
var doubled = nums.Select(n => n * 2);
Console.WriteLine($"In:{nums.Length} Out:{doubled.Count()}");Quick Check
Choose the right operator for the job.
Recap
Select projects each element into a new shape — a property, a string, an anonymous type, or a record — keeping count and order one-to-one. It has an index overload and is deferred.
SelectMany flattens nested sequences. Next we sort and group the data we have shaped.
Frequently asked questions
Is the “Select and Projection” lesson free?
Yes — the full text of “Select and Projection” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Select and Projection”?
Shape data into new forms. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Select and Projection” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Where and Filtering
- Select and Projection
- OrderBy and Grouping
- Aggregates and ToList