0
0
C Sharp (C#)programming~5 mins

Select clause projection in C Sharp (C#)

Choose your learning style9 modes available
Introduction

The select clause projection helps you pick and shape data from a list or collection into a new form.

When you want to get only certain parts of objects from a list.
When you want to create a new list with transformed or combined data.
When you want to simplify complex objects into simpler ones for display.
When you want to extract specific fields from a collection of records.
Syntax
C Sharp (C#)
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.

Examples
Selects only the Name property from each person.
C Sharp (C#)
var names = from person in people
            select person.Name;
Creates new objects with Name and Age from each person.
C Sharp (C#)
var nameAndAge = from person in people
                 select new { person.Name, person.Age };
Selects the Name in uppercase letters.
C Sharp (C#)
var upperNames = from person in people
                 select person.Name.ToUpper();
Sample Program

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.

C Sharp (C#)
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; }
}
OutputSuccess
Important Notes

You can use select to create anonymous types with new shapes.

The original collection is not changed; select creates a new sequence.

Summary

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.