Introduction
The select clause projection helps you pick and shape data from a list or collection into a new form.
Jump into concepts and practice - no test required
The select clause projection helps you pick and shape data from a list or collection into a new form.
var result = from item in collection
select new { item.Property1, item.Property2 };The select keyword defines what data to keep or create.
You can create new anonymous objects or select existing properties.
Name property from each person.var names = from person in people
select person.Name;Name and Age from each person.var nameAndAge = from person in people
select new { person.Name, person.Age };Name in uppercase letters.var upperNames = from person in people
select person.Name.ToUpper();This program creates a list of people, then uses select clause projection to get their names and ages in a new form. It prints each person's name and age.
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 }, new Person { Name = "Charlie", Age = 35 } }; var nameAndAge = from person in people select new { person.Name, person.Age }; foreach (var item in nameAndAge) { Console.WriteLine($"Name: {item.Name}, Age: {item.Age}"); } } } class Person { public string Name { get; set; } public int Age { get; set; } }
You can use select to create anonymous types with new shapes.
The original collection is not changed; select creates a new sequence.
Select clause projection picks and shapes data from collections.
It helps create new objects or extract specific properties.
Use it to simplify or transform data for easier use.
select clause do in a LINQ query in C#?select in LINQselect clause defines what data to return from the query, shaping the output.where, sorting by orderby, grouping by group by. Only select shapes the output.select to get only the names from a list of Person objects?Name property is select p.Name.where incorrectly without a condition. var names = from p in people orderby p.Name select p; selects whole object, not just names. var names = from p in people group p by p.Name; groups data, not selects.var numbers = new[] {1, 2, 3};
var result = from n in numbers select n * 2;
foreach(var r in result) Console.Write(r + " ");var people = new List<Person>(); var names = from p in people select p.Name.ToUpper;
ToUpper is a method and requires parentheses: ToUpper().Name is valid. List<Person> cannot be queried with LINQ. is wrong because List supports LINQ. select clause must come before from clause. is wrong because from must come first.Name and Price, how would you use a LINQ select clause to create a new list of anonymous objects containing only Name and a discounted price (10% off)?new { } with property assignments inside select.Name and discounted price. var discounted = from p in products select p.Name, p.Price * 0.9; syntax is invalid (multiple values without new). var discounted = from p in products where p.Price > 0.9 select p.Name; filters but does not create discounted price. var discounted = from p in products select new List { p.Name, p.Price * 0.9 }; tries to create a List incorrectly.