linq
  1. linq-projection-and-transformation

Projection and Transformation - LINQ Method Syntax

In LINQ, projection and transformation are the processes of selecting and transforming data from a source collection and creating a new collection of values. Projection refers to selecting specific properties or fields from objects in the source collection, while transformation refers to performing an operation on each element in the source collection and creating a new collection of the transformed values.

Syntax

// Projection Syntax
IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

// Transformation Syntax
IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);

Example

// Projection Example
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Select(n => n * 2);

// Transformation Example
var persons = new List<Person> { new Person { Name = "John", Age = 30 }, new Person { Name = "Jane", Age = 25 } };
var personNames = persons.Select((p, index) => $"Person {index}: {p.Name} ({p.Age})");

In the above examples, we used the Select method to project and transform data from source collections. In the first example, we projected even numbers by multiplying each number by 2. In the second example, we transformed a collection of Person objects into a collection of strings that contain person names and ages.

Output

After running the above code, the output will be:

// Projection Output
[2, 4, 6, 8, 10]

// Transformation Output
["Person 0: John (30)", "Person 1: Jane (25)"]

Explanation

In the projection example, we used the Select method to project every even number by multiplying each number by 2. The Select method takes a function that takes an input of the source collection type and returns the projected value.

In the transformation example, we used the Select method to transform every Person object into a string that contains person name and age. The Select method takes a function that takes an input of the source collection type and index, and returns the transformed value.

Use

Projection and transformation are used when we need to obtain a subset of data from a larger data set or transform data into a new format that suits our requirements. This technique is useful when we need to manipulate data in specific ways before using it in our application.

Important Points

  • Projection and transformation methods do not modify the original source collection.
  • The projection and transformation methods operate lazily. That means they only transform the data from the source collection when the result is needed.
  • Projection and transformation support the use of anonymous types to create new objects and define custom types for transformed data.

Summary

Projection and transformation are essential techniques in LINQ that allow us to select and transform data from a source collection. We've seen how these techniques are used with the Select method in C# to project and transform data from a source collection. Projection and transformation are valuable techniques as they help developers manipulate data in specific ways before using it in their application, and they support the use of anonymous types and custom types to define new objects and transformed data.

Published on: