📅  最后修改于: 2023-12-03 14:39:43.415000             🧑  作者: Mango
LINQ (Language-Integrated Query) is a powerful feature in C# that allows developers to write expressive and concise code for querying data from different sources such as arrays, collections, databases, and XML documents. In this tutorial, we'll focus on the Select
method in LINQ that is used to transform the elements of a sequence into a new sequence by applying a selector function to each element.
Before we dive into the Select
method, ensure you have the following requirements:
Select
is an extension method defined in the System.Linq
namespace. It returns a new sequence of elements that are obtained by applying a transformation function to each element of the source sequence. The transformation function takes an element of the source sequence as an input and returns a new element that is added to the output sequence.
The basic syntax of the Select
method is as follows:
IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector
)
source
: The sequence of elements to be projected.selector
: The transformation function that is applied to each element.The Select
method takes two parameters: the source
sequence and the selector
function that defines the transformation to be applied. When we call Select
on a sequence, it returns a new sequence that contains the transformed elements.
Let's explore some examples to see how we can use the Select
method.
Consider the following example where we have a list of integers and we want to select only the even numbers:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
var evenNumbers = numbers.Select(n => n % 2 == 0);
In this example, we create a List<int>
called numbers
that contains six integer values. We then call the Select
method on the numbers
sequence and pass a lambda expression that checks if the number is even or not using the modulus operator. The result is a sequence of boolean values indicating whether each number is even or not.
Consider the following example where we have a list of Employee objects and we want to select only their names:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Department { get; set; }
}
List<Employee> employees = new List<Employee>
{
new Employee { ID = 1, Name = "John", Age = 30, Department = "IT" },
new Employee { ID = 2, Name = "Jane", Age = 25, Department = "HR" },
new Employee { ID = 3, Name = "Jack", Age = 40, Department = "Sales" }
};
var employeeNames = employees.Select(e => e.Name);
In this example, we define a class called Employee
that has four properties: ID
, Name
, Age
, and Department
. We then create a List<Employee>
called employees
that contains three Employee objects. We then call the Select
method on the employees
sequence and pass a lambda expression that selects only the Name
property of each Employee object. The result is a new sequence that contains only the names of the employees.
The Select
method in LINQ is a powerful tool for transforming sequences of data. We can use it to select specific elements from a sequence, transform them into a new sequence of elements, or even combine them with other sequences using other LINQ methods. With this tutorial, you should have a basic understanding of how to use the Select
method in C# LINQ.