文章目录
Select
当谈到 C# LINQ 的 Select 方法时,它是一个非常强大的工具,可以用于各种不同的用途。下面是一些常见的用法:
- 投影(Projection)
Select 最常见的用途是对集合中的每个元素执行转换操作,从而创建一个新的序列。这可以用来选择特定的属性、计算派生值或执行任何自定义的转换逻辑。
List<int> numbers = new List<int> {
1, 2, 3, 4, 5 };
List<string> strings = numbers.Select(n => "Number " + n.ToString()).ToList();
在上述示例中,我们使用 Select 将整数列表 numbers 转换为字符串列表 strings,其中每个元素都以 "Number " 开头。
- 投影到匿名类型(Projection to Anonymous Type)
Select 可以用于将元素投影到匿名类型,方便地创建一个只包含所需属性的新对象。
List<Person> people = new List<Person>
{
new Person {
Name = "Alice", Age = 25 },
new Person {
Name = "Bob", Age = 30 },
new Person {
Name = "Charlie", Age = 40 }
};
var anonymousObjects = people.Select(p => new {
FirstName = p.Name, AgeCategory = p.Age <= 30 ? "Young" : "Old" }).ToList();
在上述示例中,我们有一个包含人员信息的人员列表 people。使用 Select,我们将每个人员投影到一个匿名类型中,并只选择 Name 和 AgeCategory 属性。
- 字符串连接(String Concatenation)
使用 Select 方法和字符串插值(string interpolation),我们可以将集合中的元素连接为单个字符串。
List<string> animals = new List<string> {
"cat", "dog", "bird" };
string concatenatedString = string.Join(", ", animals.Select(a => $"I love {
a}"));
在上述示例中,我们使用 Select 将集合中的每个动物名称转换为一个带有字符串插值的句子,并使用 string.Join 将这些句子连接为一个字符串。
- 聚合(Aggregation)
Select 还可以与其他聚合函数(例如 Sum、Max、Min、Average 等)结合使用,以对集合中的元素进行计算汇总。
List<int> numbers = new List<int> {
1, 2, 3, 4, 5 };
int sumOfSquares = numbers.Select(n => n * n).Sum();
int maxNumber = numbers.Select(n => n * 2).Max();
在上述示例中,我们使用 Select 将每个数字平方,并使用 Sum 和 Max 方法分别计算平方值的总和和最大值。
SelectMany
SelectMany 是 LINQ 中的一个方法,用于将嵌套的集合(集合的集合)转换为单个扁平的序列。它可以将嵌套结构的序列展平为单个序列,使操作更便捷。
以下是 SelectMany 方法的语法:
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector);
SelectMany 方法接受一个源序列以及一个返回子序列的函数,并将每个元素的子序列连接起来。它返回一个实现了 IEnumerable 接口的延迟执行的查询结果序列。
这是一个使用 SelectMany 方法的示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<List<int>> nestedNumbers = new List<List<int>>
{
new List<int> {
1, 2, 3 },
new List<int> {
4, 5, 6 },
new List<int> {
7, 8, 9 }
};
// 使用 SelectMany 方法将所有子序列连接成一个平坦的序列
IEnumerable<int> flattenedNumbers = nestedNumbers.SelectMany(x => x);
// 遍历平坦的序列
foreach (int number in flattenedNumbers)
{
Console.WriteLine(number);
}
}
}
在上述示例中,我们创建了一个嵌套的整数列表 nestedNumbers,其中包含三个子列表。然后,我们使用 SelectMany 方法将这些子列表连接成一个平坦的整数序列。
Take
在 C# 的 LINQ 中,Take 是用于从集合中获取指定数量的元素的方法。它可以用来限制查询结果的大小或获取集合中的前几个元素。
Take 方法的语法如下:
IEnumerable<T> Take<T>(this IEnumerable<T> source, int count)
其中,source 是要操作的集合本身,count 是要获取的元素数量。
下面是 Take 方法的几个用法示例:
- 获取集合的前几个元素
List<int> numbers = new List<int> {
1, 2, 3, 4, 5 };
IEnumerable<int> firstThreeNumbers = numbers.Take(3);
在上述示例中,我们使用 Take 方法获取了 numbers 集合的前三个元素(即 1、2 和 3)。
- 限制查询结果的大小
List<string> names = new List<string> {
"Alice", "Bob", "Charlie", "David", "Eve" };
IEnumerable<string> limitedNames = names.Take(2);
在上述示例中,我们使用 Take 方法限制了查询结果最多只能包含前两个元素(即 “Alice” 和 “Bob”)。
- 与其他 LINQ 方法组合使用
List<int> numbers = new List<int> {
1, 2, 3, 4, 5 };
IEnumerable<int> evenNumbers = numbers.Where(n => n % 2 == 0).Take(2);
在上述示例中,我们先使用 Where 方法筛选出偶数,然后再使用 Take 方法获取前两个偶数。
需要注意的是,Take 方法不会修改原始集合,它只返回一个包含指定数量元素的新的可枚举序列。
TakeWhile
在 TakeWhile 方法中,它会根据指定的条件逐个获取元素,直到遇到第一个不满足条件的元素为止。
以下是正确的示例:
List<int> numbers = new List<int> {
2, 4, 6, 1, 8, 10 };
IEnumerable<int> consecutiveEvenNumbers = numbers.TakeWhile(n => n % 2 == 0);
在此示例中,使用 TakeWhile 方法获取 numbers 列表中连续的偶数元素。返回的结果是 2、4 和 6,因为在第一个奇数 1 出现后,就停止获取。
Take
在LINQ中,Skip方法用于跳过序列中指定数量的元素,并返回剩余的元素。它允许你忽略集合的前几个元素,从指定位置开始获取后续的元素。
Skip方法的语法如下:
IEnumerable<T> Skip<T>(this IEnumerable<T> source, int count)
其中,source是要操作的集合本身,count是要跳过的元素数量。
跳过指定数量的元素:
List<int> numbers = new List<int> {
1, 2, 3, 4, 5 };
IEnumerable<int> remainingNumbers = numbers.Skip(2);
在上述示例中,我们使用Skip方法跳过了集合numbers中的前两个元素。返回的结果是3、4、5。
SkipWhile
在LINQ中,SkipWhile方法用于跳过序列中满足指定条件的连续元素,并返回剩余的元素。它允许你跳过集合中一系列符合条件的元素,直到遇到不满足条件的元素为止。
SkipWhile方法的语法如下:
IEnumerable<T> SkipWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate)
其中,source是要操作的集合本身,predicate是一个用于确定是否要跳过当前元素的函数。
以下是SkipWhile方法的几个用法示例:
- 跳过满足条件的连续元素:
List<int> numbers = new List<int> {
1, 2, 3, 4, 5 };
I