IEnumerable numbers = new List { 1, 2, 3, 4, 5 }; IEnumerable evenNumbers = numbers.Where(x => x % 2 == 0);
Package Library: System.Linq
Description: The Select method is an extension method provided by the System.Linq namespace in C# that is used to select the elements from a collection based on a specified condition and return a new sequence with only the selected elements.
Example: In the above code, the Select method is not used directly. Instead, we have used the Where method to filter out even numbers from the list of numbers and created a new sequence with only the even number. However, we can achieve the same result using the Select method as well:
IEnumerable numbers = new List { 1, 2, 3, 4, 5 }; IEnumerable evenNumbers = numbers.Select(x => x % 2 == 0);
2. Using Lambda Expression:
int[] numbers = { 1, 2, 3, 4, 5 }; var result = numbers.Select(n => n * n);
Package Library: System.Linq
Description: The Select method can also be used to transform each element of a collection into a new form using a lambda expression and return a new sequence with the transformed elements.
Example: In the above code, we have used the Select method to square each element of the array and return a new sequence with squared elements. Here, the lambda expression n => n * n is used to perform the transformation.
Description: The Select method can also be used in conjunction with the Method Syntax to select the elements from a collection based on a specified condition and return a new sequence with only the selected elements.
Example: In the above code, we have used the Method Syntax to convert each element of the array to uppercase using the Select method. Here, the lambda expression Fruit => Fruit.ToUpper() is used to perform the transformation.
C# (CSharp) Select.Add - 22 examples found. These are the top rated real world C# (CSharp) examples of Select.Add from package dev extracted from open source projects. You can rate examples to help us improve the quality of examples.