Select and Projection
Shape data into new forms.
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);All lessons in this course
- Where and Filtering
- Select and Projection
- OrderBy and Grouping
- Aggregates and ToList