C# Enumerable类 之 集合操作

总目录


前言

在 C# 中,System.Linq.Enumerable 类是 LINQ(Language Integrated Query)的核心组成部分,它提供了一系列静态方法,用于操作实现了 IEnumerable 接口的集合。通过这些方法,我们可以轻松地对集合进行查询、转换、排序和聚合等操作。

本文属于 C# Enumerable类 使用详解 中的一个章节,着重介绍 C# Enumerable 类中集合操作这部分的内容。


一、概览

方法描述示例
Concat序列合并(不去重)list1.Concat(list2);
UnionUnionBy序列并集(去重)list1.Union(list2);
ExceptExceptBy 序列差集list1.Except(list2);
IntersectIntersectBy 序列交集list1.Intersect(list2);

二、Concat :合并(不去重)

1. 什么是 Concat

Concat 是 LINQ 提供的一个扩展方法,用于将两个序列连接在一起,并返回一个包含所有元素的新序列。需要注意的是,Concat 方法不会去除重复项,如果需要去重可以使用 Union 方法。

2. Concat 方法 基本信息

1) Concat

public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second)
  • 参数
    • first:第一个要连接的序列。
    • second:第二个要连接的序列。
  • 返回值Concat 方法返回一个 IEnumerable<T> 对象,其中 T 是输入序列中元素的类型。

2)工作原理

Concat 方法通过惰性求值和迭代器模式,将两个序列依次遍历并返回一个新的序列,过程中不会创建新的集合对象,而是逐个返回两个序列中的元素。

3)使用场景

适用于合并多个集合、处理流式数据、数据预处理等场景。

3. 使用示例

示例 1:基本连接

假设我们有两个整数列表,我们希望将它们连接在一起形成一个新的列表。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3 };List<int> list2 = new List<int> { 4, 5, 6 };// 使用 Concat 连接两个列表var concatenatedList = list1.Concat(list2);Console.WriteLine(string.Join(",",concatenatedList));// 输出:1,2,3,4,5,6}
}

示例 2:不同类型的序列

虽然 Concat 方法要求两个序列的元素类型相同,但可以通过泛型和隐式转换来处理不同类型的数据。

class Program
{static void Main(){List<string> list1 = new List<string> { "Apple", "Banana" };List<object> list2 = new List<object> { 1, 2.5, true };// 使用 Concat 连接两个不同类型的列表var concatenatedList = list1.Cast<object>().Concat(list2);Console.WriteLine(string.Join(",",concatenatedList));// 输出:Apple,Banana,1,2.5,True}
}

示例 3:空序列

Concat 方法也可以用于将一个非空序列与一个空序列连接。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3 };List<int> list2 = new List<int>();// 使用 Concat 连接非空序列和空序列var concatenatedList = list1.Concat(list2);Console.WriteLine(string.Join(",",concatenatedList));// 输出:1,2,3}
}

示例 4:合并多个集合

当需要合并多个集合时,可以多次使用 Concat 方法,或者使用 Union 方法来去除重复项。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3 };List<int> list2 = new List<int> { 3, 4, 5, 6 };List<int> list3 = new List<int> { 6, 7, 8, 9 };// 使用 Concat 合并多个集合var concatenatedList = list1.Concat(list2).Concat(list3);Console.WriteLine(string.Join(",", concatenatedList));// 输出:1,2,3,3,4,5,6,6,7,8,9// 使用 Union 去除重复项var unionedList = list1.Union(list2).Union(list3);Console.WriteLine(string.Join(",", unionedList));// 输出:1,2,3,4,5,6,7,8,9}
}

示例 5:处理流式数据

由于 Concat 方法采用惰性求值,因此非常适合处理流式数据或无限序列。

class Program
{static void Main(){IEnumerable<int> streamData = GenerateStreamData();List<int> additionalData = new List<int> { 10, 20, 30 };// 使用 Concat 连接流式数据和附加数据var concatenatedStream = streamData.Concat(additionalData);// 输出结果Console.WriteLine("Concatenated Stream Data (First 10 elements):");foreach (var item in concatenatedStream.Take(10)){Console.Write(item + " ");}}static IEnumerable<int> GenerateStreamData(){int i = 0;while (true){yield return i++;}}
}

输出结果:

Concatenated Stream Data (First 10 elements):
0 1 2 3 4 5 6 7 8 9

示例 6:数据预处理

在某些情况下,你可能需要对数据进行预处理,然后再将其与其他数据连接起来。

using System;
using System.Collections.Generic;
using System.Linq;class Program
{static void Main(){List<int> rawData = new List<int> { 1, 2, 3, 4, 5 };List<int> processedData = rawData.Select(x => x * 2).ToList();List<int> additionalData = new List<int> { 10, 20, 30 };// 使用 Concat 连接预处理后的数据和其他数据var concatenatedData = processedData.Concat(additionalData);// 输出结果Console.WriteLine("Concatenated Data:");foreach (var item in concatenatedData){Console.Write(item + " ");}}
}

输出结果:

Concatenated Data:
2 4 6 8 10 10 20 30

4. 注意事项

尽管 Concat 方法非常有用,但在实际应用中也有一些需要注意的地方:

  • 空集合处理: 如果源集合为空,Concat 方法将返回一个空的结果集。因此,在使用 Concat 方法之前,通常不需要检查集合是否为空。
  • 数据类型一致:适用于需要合并的集合对象的数据类型是一样的情况。

三、UnionUnionBy:并集(去重)

在这里插入图片描述

1. 什么是 Union/UnionBy

  • Union 是 LINQ 提供的一个扩展方法,用于将两个序列合并成一个新的序列,并去除重复的元素。这个方法非常有用,特别是在需要合并多个集合并确保结果中没有重复项时。
  • UnionBy 是 .NET 6 及以上版本中引入的一个扩展方法,用于合并两个序列并根据指定的键选择器函数去除重复项。与 Union 方法不同的是,UnionBy 允许你通过一个自定义的键来确定元素是否相同,而不是直接比较整个对象。

2. Union/UnionBy 方法 基本信息

1) Union/UnionBy

public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second
)public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second,IEqualityComparer<TSource> comparer
)
  • 参数
    • first:第一个要合并的序列。
    • second:第二个要合并的序列。
    • comparer(可选):一个 IEqualityComparer<TSource> 实现,用于比较元素是否相等。
  • 返回值Union 方法返回一个 IEnumerable<T> 对象,其中 T 是输入序列中元素的类型。
public static IEnumerable<TSource> UnionBy<TSource, TKey>(this IEnumerable<TSource> first,IEnumerable<TSource> second,Func<TSource, TKey> keySelector
)public static IEnumerable<TSource> UnionBy<TSource, TKey>(this IEnumerable<TSource> first,IEnumerable<TSource> second,Func<TSource, TKey> keySelector,IEqualityComparer<TKey> comparer
)
  • 参数
    • first:第一个要合并的序列。
    • second:第二个要合并的序列。
    • keySelector:一个函数,用于从每个元素中提取键值。
    • comparer(可选):一个 IEqualityComparer 实现,用于比较键值是否相等。
  • 返回值UnionBy 方法返回一个 IEnumerable<TSource> 对象,其中 TSource 是输入序列中元素的类型。

2)工作原理

  • Union 方法通过惰性求值和迭代器模式,将两个序列合并成一个新的序列,并在遍历过程中自动去除重复元素。
  • UnionBy 方法通过指定的键选择器函数从两个序列中提取键值,并根据这些键值合并序列,去除重复项,最终返回一个包含唯一键值元素的新序列。

3)使用场景

适用于合并多个集合、处理流式数据、数据预处理、复杂数据结构合并等场景。

3. 使用示例

示例 1:基本合并

假设我们有两个整数列表,我们希望将它们合并在一起形成一个新的列表,并去除重复项。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3 };List<int> list2 = new List<int> { 3, 4, 5 };// 使用 Union 合并两个列表并去除重复项var unionedList = list1.Union(list2);Console.WriteLine(string.Join(",", unionedList));// 输出:1,2,3,4,5// 使用 UnionBy 合并两个列表并去除重复项unionedList = list1.UnionBy(list2, x => x);Console.WriteLine(string.Join(",", unionedList));// 输出:1,2,3,4,5}
}

在这个例子中,我们使用 Union / UnionBy 方法将两个整数列表合并在一起,并打印了结果。注意,数字 3 只出现了一次。

示例 2:自定义对象

当处理自定义对象时,Union 方法默认使用对象的 EqualsGetHashCode 方法来判断元素是否相同。如果你有更复杂的比较需求,可以提供自定义的 IEqualityComparer<T> 实现。

  • 使用默认的 EqualsGetHashCode 方法,并不能实现对象内容的比较
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}
class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 Union 合并两个列表并去除重复项var unionedPeople = list1.Union(list2);Console.WriteLine(string.Join(",",unionedPeople));// 输出:Alice (30),Bob (25),Charlie (30),Bob (25)}
}
  • 重写 对象的 EqualsGetHashCode 方法,可以实现对象内容的比较。使用Union时,默认就会调用重写后的方法判断对象是否相等
class Person
{public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return Name == other.Name && Age == other.Age;}return false;}public override int GetHashCode(){return HashCode.Combine(Name, Age);}public override string ToString(){return $"{Name} ({Age})";}
}
class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 Union 合并两个列表并去除重复项var unionedPeople = list1.Union(list2);Console.WriteLine(string.Join(",",unionedPeople));// 输出:Alice (30),Bob (25),Charlie (30)}
}
  • 使用自定义的 PersonComparer 来比较 Person 对象,并确保合并后的结果中没有重复项。
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}
class PersonComparer : IEqualityComparer<Person>
{public bool Equals(Person x, Person y){if (x == null || y == null) return false;return x.Name == y.Name && x.Age == y.Age;}public int GetHashCode(Person obj){return HashCode.Combine(obj.Name, obj.Age);}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{`在这里插入代码片`new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 Union 合并两个列表并去除重复项var unionedPeople = list1.Union(list2,new PersonComparer());Console.WriteLine(string.Join(",",unionedPeople));// 输出:Alice (30),Bob (25),Charlie (30)}
}
  • 使用UnionBy ,通过指定键选择器函数来确定哪些对象应该被视为相同的。并确保合并后的结果中没有重复项。
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 UnionBy 合并两个列表并根据年龄去重var unionedPeople = list1.UnionBy(list2, p => p.Age);Console.WriteLine(string.Join(",", unionedPeople));// 输出:Alice (30),Bob (25)}
}

示例 3:处理无限序列

Union /UnionBy方法采用惰性求值(Lazy Evaluation),这意味着它不会立即执行合并操作,而是等到实际遍历时才会计算结果。这使得 Union /UnionBy 可以处理无限序列或延迟执行复杂的查询。

using System;
using System.Collections.Generic;
using System.Linq;class Program
{static void Main(){IEnumerable<int> infiniteNumbers = Enumerable.Range(0, int.MaxValue).Select(i => i * 2);List<int> finiteNumbers = new List<int> { 1, 3, 5 };// 使用 Union 合并无限序列和有限序列var unionedSequence = infiniteNumbers.Union(finiteNumbers);// var unionedSequence = infiniteNumbers.UnionBy(finiteNumbers, x => x);// 限制输出数量Console.WriteLine("Unioned Sequence (First 10 elements):");foreach (var item in unionedSequence.Take(10)){Console.Write(item + " ");}}
}

输出结果:

Unioned Sequence (First 10 elements):
0 2 4 6 8 10 12 14 16 18

在这个例子中,我们展示了如何使用 Union /UnionBy 方法合并一个无限序列和一个有限序列,并通过 Take 方法限制输出的数量,从而避免无限循环。

示例 4:自定义比较器

当处理复杂的数据结构时,可以通过自定义比较器来实现更灵活的合并逻辑。

class Person
{public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return Name == other.Name && Age == other.Age;}return false;}public override int GetHashCode(){return HashCode.Combine(Name, Age);}public override string ToString(){return $"{Name} ({Age})";}
}class PersonComparerByName : IEqualityComparer<Person>
{public bool Equals(Person x, Person y){if (x == null || y == null) return false;return x.Name == y.Name;}public int GetHashCode(Person obj){return obj.Name?.GetHashCode() ?? 0;}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Alice", Age = 31 },new Person { Name = "Charlie", Age = 30 }};// 使用 Union 合并两个列表并根据名字去重var unionedPeople = list1.Union(list2, new PersonComparerByName());Console.WriteLine(string.Join(",", unionedPeople));// 输出:Alice (30),Bob (25),Charlie (30)}
}

在这个例子中,我们展示了如何使用自定义的 PersonComparerByName 来根据名字去重合并 Person 对象。

class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class CustomAgeComparer : IEqualityComparer<int>
{public bool Equals(int x, int y){// 自定义比较逻辑:年龄相差不超过1岁视为相同return Math.Abs(x - y) <= 1;}public int GetHashCode(int obj){return 0;}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "David", Age = 26 }};// 使用 UnionBy 合并两个列表并根据年龄和自定义比较器去重var unionedPeople = list1.UnionBy(list2, p => p.Age, new CustomAgeComparer());Console.WriteLine(string.Join(",", unionedPeople));// 输出:Alice (30),Bob (25)}
}

示例 5:多个集合的合并

当需要合并多个集合时,可以多次使用 Union 方法。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3 };List<int> list2 = new List<int> { 4, 5, 6 };List<int> list3 = new List<int> { 7, 8, 9 };// 使用 Union 合并多个集合var unionedLists = list1.Union(list2).Union(list3);// var unionedLists = list1.UnionBy(list2, x => x).UnionBy(list3, x => x);Console.WriteLine(string.Join(",", unionedLists));// 输出:1,2,3,4,5,6,7,8,9}
}

4. 注意事项

尽管 Union/UnionBy 方法非常有用,但在实际应用中也有一些需要注意的地方:

  • 空集合处理: 如果源集合为空,Union/UnionBy 方法将返回一个空的结果集。因此,在使用 Union /UnionBy方法之前,通常不需要检查集合是否为空。
  • 数据类型一致:适用于需要合并的集合对象的数据类型是一样的情况。

四、ExceptExceptBy :差集

在这里插入图片描述

1. 什么是 Except/ExceptBy

  • Except 方法用于计算两个序列的差集,即返回第一个序列中存在但第二个序列中不存在的所有元素。默认情况下,它使用对象的默认比较器(如 EqualityComparer.Default)来比较元素是否相等。你也可以提供自定义的比较器来控制比较逻辑。
  • ExceptBy 是 .NET 6 及以上版本中引入的一个扩展方法,用于计算两个序列的差集,但与 Except 不同的是,它允许你通过指定的键选择器函数来确定元素是否相同。这意味着你可以根据某个特定属性或计算结果来进行差集操作,而不是直接比较整个对象。

2. Except/ExceptBy 方法 基本信息

1) Except/ExceptBy

public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second
)public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second,IEqualityComparer<TSource> comparer
)
  • 参数
    • first:第一个要计算差集的序列。
    • second:第二个要计算差集的序列。
    • comparer(可选):一个 IEqualityComparer<TSource> 实现,用于比较元素是否相等。
  • 返回值Except 方法返回一个 IEnumerable<TSource> 对象,其中 TSource 是输入序列中元素的类型。
public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> first,IEnumerable<TKey> second,Func<TSource, TKey> keySelector
)public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> first,IEnumerable<TKey> second,Func<TSource, TKey> keySelector,IEqualityComparer<TKey> comparer
)
  • 参数
    • first:第一个要计算差集的序列。
    • second:第二个要计算差集的序列(注意这里的元素类型是键类型 TKey)。
    • keySelector:一个函数,用于从每个元素中提取键值。
    • comparer(可选):一个 IEqualityComparer 实现,用于比较键值是否相等。
  • 返回值UnionBy 方法返回一个 IEnumerable<TSource> 对象,其中 TSource 是输入序列中元素的类型。

2)工作原理

  • Except:计算两个序列的差集,返回第一个序列中存在但第二个序列中不存在的所有元素,并使用默认或自定义的比较器来判断元素是否相等。
  • ExceptBy:根据指定的键选择器函数提取键值,并计算两个序列的差集,返回第一个序列中存在但第二个序列中不存在的所有元素,去除具有相同键值的重复项。

3)使用场景

适用于数据过滤、数据预处理、多个集合的差集计算等场景。

3. 使用示例

示例 1:基本差集计算

假设我们有两个整数列表,我们希望计算第一个列表中存在但第二个列表中不存在的所有元素。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };List<int> list2 = new List<int> { 3, 4, 6 };// 使用 Except 计算差集var exceptList = list1.Except(list2);Console.WriteLine(string.Join(",", exceptList));// 输出:1,2,5exceptList = list1.ExceptBy(list2, x => x);Console.WriteLine(string.Join(",", exceptList));// 输出:1,2,5}
}

示例 2:自定义对象

当处理自定义对象时,Union 方法默认使用对象的 EqualsGetHashCode 方法来判断元素是否相同。如果你有更复杂的比较需求,可以提供自定义的 IEqualityComparer<T> 实现。

  • 使用默认的 EqualsGetHashCode 方法,并不能实现对象内容的比较
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};List<Person> list2 = new List<Person>{new Person { Name = "Bob", Age = 25 },new Person { Name = "David", Age = 40 }};// 使用 Except 计算差集var exceptPeople = list1.Except(list2);Console.WriteLine(string.Join(",", exceptPeople));// 输出:Alice (30),Bob (25),Charlie (35)}
}
  • 重写 对象的 EqualsGetHashCode 方法,可以实现对象内容的比较。使用Except时,默认就会调用重写后的方法判断对象是否相等
class Person
{public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return Name == other.Name && Age == other.Age;}return false;}public override int GetHashCode(){return HashCode.Combine(Name, Age);}public override string ToString(){return $"{Name} ({Age})";}
}
class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 Except 计算差集var exceptPeople = list1.Except(list2);Console.WriteLine(string.Join(",", exceptPeople));// 输出:Alice (30)}
}
  • 使用自定义的 PersonComparer 来比较 Person 对象。
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}
class PersonComparer : IEqualityComparer<Person>
{public bool Equals(Person x, Person y){if (x == null || y == null) return false;return x.Name == y.Name && x.Age == y.Age;}public int GetHashCode(Person obj){return HashCode.Combine(obj.Name, obj.Age);}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 Except 计算差集并使用自定义比较器var exceptPeople = list1.Except(list2, new PersonComparer());Console.WriteLine(string.Join(",", exceptPeople));// 输出:Alice (30)}
}
  • 使用ExceptBy ,通过指定键选择器函数来确定哪些对象应该被视为相同的。并确保合并后的结果中没有重复项。
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};List<Person> list2 = new List<Person>{new Person { Name = "Bob", Age = 25 },new Person { Name = "David", Age = 40 }};// 使用 ExceptBy 计算差集并排除特定年龄var exceptPeople = list1.ExceptBy(list2.Select(x => x.Age), p => p.Age);Console.WriteLine(string.Join(",", exceptPeople));// 输出:Alice (30),Charlie (35)}
}

示例 3:处理无限序列

Except /ExceptBy方法采用惰性求值(Lazy Evaluation),这意味着它不会立即执行合并操作,而是等到实际遍历时才会计算结果。这使得 Except /ExceptBy 可以处理无限序列或延迟执行复杂的查询。

class Program
{static void Main(){IEnumerable<int> infiniteNumbers = Enumerable.Range(0, int.MaxValue).Select(i => i * 2);List<int> finiteNumbers = new List<int> { 1, 3, 5 };// 使用 Union 合并无限序列和有限序列//var unionedSequence = infiniteNumbers.Except(finiteNumbers);var unionedSequence = infiniteNumbers.ExceptBy(finiteNumbers, x => x);// 限制输出数量Console.WriteLine("Unioned Sequence (First 10 elements):");foreach (var item in unionedSequence.Take(10)){Console.Write(item + " ");}}
}

输出结果:

Unioned Sequence (First 10 elements):
0 2 4 6 8 10 12 14 16 18

在这个例子中,我们展示了如何使用 Except /ExceptBy 方法合并一个无限序列和一个有限序列,并通过 Take 方法限制输出的数量,从而避免无限循环。

示例 4:自定义比较器

class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class CustomAgeComparer : IEqualityComparer<int>
{public bool Equals(int x, int y){// 自定义比较逻辑:年龄相差不超过1岁视为相同return Math.Abs(x - y) <= 1;}public int GetHashCode(int obj){return 0;}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "David", Age = 26 }};// 使用 UnionBy 合并两个列表并根据年龄和自定义比较器去重var unionedPeople = list1.ExceptBy(list2.Select(x=>x.Age), p => p.Age, new CustomAgeComparer());Console.WriteLine(string.Join(",", unionedPeople));// 输出:Charlie (35)}
}

示例 5:多个集合的合并

当需要合并多个集合时,可以多次使用 Except/ExceptBy 方法。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };List<int> list2 = new List<int> { 3, 4, 6 };List<int> list3 = new List<int> { 4, 7, 8 };// 使用 Union 合并多个集合//var unionedLists = list1.Except(list2).Except(list3);var unionedLists = list1.ExceptBy(list2, x => x).ExceptBy(list3, x => x);Console.WriteLine(string.Join(",", unionedLists));// 输出:1,2,5}
}

4. 注意事项

尽管 Except/ExceptBy 方法非常有用,但在实际应用中也有一些需要注意的地方:

  • 空集合处理: 如果源集合为空,Except/ExceptBy 方法将返回一个空的结果集。因此,在使用 Except /ExceptBy 方法之前,通常不需要检查集合是否为空。
  • 数据类型一致:适用于需要取差集的集合对象的数据类型是一样的情况。

五、IntersectIntersectBy :交集

在这里插入图片描述

1. 什么是 Intersect/IntersectBy

  • Intersect 方法用于计算两个序列的交集,即返回两个序列中共有的所有元素。默认情况下,它使用对象的默认比较器(如 EqualityComparer<T>.Default)来比较元素是否相等。你也可以提供自定义的比较器来控制比较逻辑。
  • IntersectBy 是 .NET 6 及以上版本中引入的一个扩展方法,用于计算两个序列的交集,但与 Intersect 不同的是,它允许你通过指定的键选择器函数来确定元素是否相同。这意味着你可以根据某个特定属性或计算结果来进行交集操作,而不是直接比较整个对象。

2. Intersect/IntersectBy 方法 基本信息

1) Intersect/IntersectBy

public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second
)public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second,IEqualityComparer<TSource> comparer
)
  • 参数
    • first:第一个要计算交集的序列。
    • second:第二个要计算交集的序列。
    • comparer(可选):一个 IEqualityComparer<TSource> 实现,用于比较元素是否相等。
  • 返回值Except 方法返回一个 IEnumerable<TSource> 对象,其中 TSource 是输入序列中元素的类型。
public static IEnumerable<TSource> IntersectBy<TSource, TKey>(this IEnumerable<TSource> first,IEnumerable<TKey> second,Func<TSource, TKey> keySelector
)public static IEnumerable<TSource> IntersectBy<TSource, TKey>(this IEnumerable<TSource> first,IEnumerable<TKey> second,Func<TSource, TKey> keySelector,IEqualityComparer<TKey> comparer
)
  • 参数
    • first:第一个要计算交集的序列。
    • second:第二个要计算交集的序列(注意这里的元素类型是键类型 TKey)。
    • keySelector:一个函数,用于从每个元素中提取键值。
    • comparer(可选):一个 IEqualityComparer 实现,用于比较键值是否相等。
  • 返回值UnionBy 方法返回一个 IEnumerable<TSource> 对象,其中 TSource 是输入序列中元素的类型。

2)工作原理

  • Intersect:计算两个序列的交集,返回两个序列中共有的所有元素,并使用默认或自定义的比较器来判断元素是否相等。
  • IntersectBy:根据指定的键选择器函数提取键值,并计算两个序列的交集,返回第一个序列中具有与第二个序列中相同键值的所有元素。

3)使用场景

适用于数据过滤、数据预处理、多个集合的交集计算等场景。

3. 使用示例

示例 1:基本交集计算

假设我们有两个整数列表,我们希望找出它们共有的元素。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };List<int> list2 = new List<int> { 3, 4, 6 };// 使用 Intersect 计算交集var intersectList = list1.Intersect(list2);Console.WriteLine(string.Join(",", intersectList));// 输出:3,4intersectList = list1.IntersectBy(list2, x => x);Console.WriteLine(string.Join(",", intersectList));// 输出:3,4}
}

示例 2:自定义对象

当处理自定义对象时,Intersect 方法默认使用对象的 EqualsGetHashCode 方法来判断元素是否相同。如果你有更复杂的比较需求,可以提供自定义的 IEqualityComparer<T> 实现。

  • 使用默认的 EqualsGetHashCode 方法,并不能实现对象内容的比较
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};List<Person> list2 = new List<Person>{new Person { Name = "Bob", Age = 25 },new Person { Name = "David", Age = 40 }};// 使用 Intersect 计算交集var intersectPeople = list1.Intersect(list2);Console.WriteLine(string.Join(",", intersectPeople));// 输出:          - 表示 没有交集}
}
  • 重写 对象的 EqualsGetHashCode 方法,可以实现对象内容的比较。使用Except时,默认就会调用重写后的方法判断对象是否相等
class Person
{public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return Name == other.Name && Age == other.Age;}return false;}public override int GetHashCode(){return HashCode.Combine(Name, Age);}public override string ToString(){return $"{Name} ({Age})";}
}
class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 Intersect 计算交集var intersectPeople = list1.Intersect(list2);Console.WriteLine(string.Join(",", intersectPeople));// 输出:Bob (25)}
}
  • 使用自定义的 PersonComparer 来比较 Person 对象。
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}
class PersonComparer : IEqualityComparer<Person>
{public bool Equals(Person x, Person y){if (x == null || y == null) return false;return x.Name == y.Name && x.Age == y.Age;}public int GetHashCode(Person obj){return HashCode.Combine(obj.Name, obj.Age);}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "Bob", Age = 25 }};// 使用 Intersect 计算交集并使用自定义比较器var intersectPeople = list1.Intersect(list2, new PersonComparer());Console.WriteLine(string.Join(",", intersectPeople));// 输出:Bob (25)}
}
  • 使用ExceptBy ,通过指定键选择器函数来确定哪些对象应该被视为相同的。并确保合并后的结果中没有重复项。
class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};List<Person> list2 = new List<Person>{new Person { Name = "Bob", Age = 25 },new Person { Name = "David", Age = 40 }};// 使用 IntersectBy 计算交集var intersectPeople = list1.IntersectBy(list2.Select(x => x.Age), p => p.Age);Console.WriteLine(string.Join(",", intersectPeople));// 输出:Bob (25)}
}

示例 3:处理无限序列

Intersect /IntersectBy方法采用惰性求值(Lazy Evaluation),这意味着它不会立即执行合并操作,而是等到实际遍历时才会计算结果。这使得 Intersect /IntersectBy 可以处理无限序列或延迟执行复杂的查询。

class Program
{static void Main(){IEnumerable<int> infiniteNumbers = Enumerable.Range(0, int.MaxValue).Select(i => i * 2);List<int> finiteNumbers = new List<int> { 4, 6, 8 };// 使用 Intersect 计算交集var intersectSequence = infiniteNumbers.Intersect(finiteNumbers);// 限制输出数量Console.WriteLine("Elements common between infiniteNumbers and finiteNumbers (First 3 elements):");foreach (var item in intersectSequence.Take(3)){Console.Write(item + " ");}}
}

输出结果:

Elements common between infiniteNumbers and finiteNumbers (First 3 elements):
4 6 8

在这个例子中,我们展示了如何使用 Intersect 方法计算一个无限序列和一个有限序列之间的交集,并通过 Take 方法限制输出的数量,从而避免无限循环。

示例 4:自定义比较器

class Person
{public string Name { get; set; }public int Age { get; set; }public override string ToString(){return $"{Name} ({Age})";}
}class CustomAgeComparer : IEqualityComparer<int>
{public bool Equals(int x, int y){// 自定义比较逻辑:年龄相差不超过1岁视为相同return Math.Abs(x - y) <= 1;}public int GetHashCode(int obj){return 0;}
}class Program
{static void Main(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};List<Person> list2 = new List<Person>{new Person { Name = "Charlie", Age = 30 },new Person { Name = "David", Age = 26 }};// 使用 IntersectBy 取两个列表交集并根据年龄和自定义比较器 比较Person对象var intersectPeople = list1.IntersectBy(list2.Select(x => x.Age), p => p.Age, new CustomAgeComparer());Console.WriteLine(string.Join(",", intersectPeople));// 输出:Alice (30),Bob (25)}
}

示例 5:多个集合的合并

当需要合并多个集合时,可以多次使用 Intersect/IntersectBy 方法。

class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };List<int> list2 = new List<int> { 3, 4, 6 };List<int> list3 = new List<int> { 4, 7, 8 };// 使用 Union 合并多个集合//var unionedLists = list1.Intersect(list2).Intersect(list3);var unionedLists = list1.IntersectBy(list2, x => x).IntersectBy(list3, x => x);Console.WriteLine(string.Join(",", unionedLists));// 输出:4}
}

4. 注意事项

尽管 Intersect/IntersectBy方法非常有用,但在实际应用中也有一些需要注意的地方:

  • 空集合处理: 如果源集合为空, Intersect/IntersectBy 方法将返回一个空的结果集。因此,在使用 Intersect/IntersectBy方法之前,通常不需要检查集合是否为空。
  • 数据类型一致:适用于需要取差集的集合对象的数据类型是一样的情况。

结语

回到目录页:C#/.NET 知识汇总
希望以上内容可以帮助到大家,如文中有不对之处,还请批评指正。


参考资料:
微软官方文档 Enumerable

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/32259.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网络安全 api 网络安全 ast技术

随着应用或者API被攻击利用已经越来越多&#xff0c;虽然来自开源组件的漏洞加剧了这一现象的发生&#xff0c;但是&#xff0c;其实主要还是在于应用程序或者API本身没有做好防范&#xff0c;根源在于源代码本身的质量没有严格把控。AST是指Application Security Testing&…

【2025前端高频面试题——系列一之MVC和MVVM】

前端高频面试题——系列一之MVC和MVVM 前言一、MVC的基本逻辑二、MVVM的基本逻辑总结 提示&#xff1a;片尾总结了要点&#xff0c;硬背的话直接跳到最后 前言 相信持续关注我文章的小伙伴知道我之前就MVC和MVVM做过较为详细的讲解&#xff0c;但是我发现&#xff0c;他依旧是…

axure11安装教程包含下载、安装、汉化、授权(附安装包)图文详细教程

文章目录 前言一、axure11安装包下载二、axure11安装教程1.启动安装程序2.安装向导界面3.安装协议协议页面2.选择安装位置3.开始安装4.完成安装 三、axure11汉化教程1.axure11汉化包2.axure11汉化设置 四、axure11授权教程1.打开axure112.设置使用方式3.输入许可证号4.axure11安…

Ubuntu 22.04使用pigz多线程快速解压/压缩文件

最近搞项目&#xff0c;资料太大&#xff0c;解压时间太久&#xff0c;于是想办法解决。 开贴记录。 1.安装pigz sudo apt install pigz 2.解压资料 解压命令为 tar --use-compress-programpigz -xvpf ***.tar.gz 将最后的部分***.tar.gz换成你自己的文件即可 例如 ti…

版本控制器Git(3)

文章目录 前言一、分支管理策略二、Bug分支管理遇到Bug时的处理方法使用 git stash 暂存工作区内容创建并切换到Bug修复分支恢复之前的工作 三、临时分支的删除总结 前言 我们在上篇讲到了分支&#xff0c;现在我们就着这个继续来讲解&#xff01; 一、分支管理策略 master分支…

pytest+allure+jenkins

本地运行参考&#xff1a;pytestallure 入门-CSDN博客 jenkins运行如下&#xff1a; 安装插件&#xff1a;allure 配置allure安装目录 配置pytest、allure 环境变量 配置流水线 进行build,结果如下 ,点击allure report 查看结果

AI术语整理(持续更新)

在AI相关的学习和使用中&#xff0c;经常会被各种术语混淆&#xff0c;搞清楚术语本来的含义是关键&#xff0c;本文收集整理了部分AI相关术语&#xff0c;会持续更新。 基础概念 人工智能&#xff08;Artificial Intelligence&#xff0c;AI&#xff09;&#xff1a;一种模…

【蓝桥杯单片机】第十一届省赛

一、真题 二、创建工程 1.在C盘以外的盘新建文件夹&#xff0c;并在文件夹里面创建两个文件夹Driver 和Project 2.打开keil软件&#xff0c;在新建工程并选择刚刚建好的project文件夹&#xff0c;以准考证号命名 3.选择对应的芯片型号 4.选择否&#xff0c;即不创建启动文件 …

GMT绘图笔记:用深度作为表面高度(Z 值),用其他物理量(泊松比)给表面着色

之前用GMT绘制莫霍面的三维示意图是用的莫霍面的深度作为表面着色。 GMT绘图笔记&#xff1a;绘制堆叠三维图_gmt画图-CSDN博客 如果要换成其他的物理场&#xff0c;比如泊松比&#xff0c;则需要使用以下的代码 gmt begin BS_figures2 png,pdf E600 # 绘制底图 # Chistrong…

docker搭建elk

文章目录 1.拉取镜像2.ES配置3.logstash配置4.kibana配置5.创建自定义网络6.docker-compose.yml文件7.springboot对接Logstash1.创建一个springboot项目引入主要依赖2.application.yml配置3.resources目录中新增logback-spring.xml4.启动项目&#xff0c;搞点日志5.进入kibana控…

golang算法快慢指针

876. 链表的中间结点 给你单链表的头结点 head &#xff0c;请你找出并返回链表的中间结点。 如果有两个中间结点&#xff0c;则返回第二个中间结点。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5] 输出&#xff1a;[3,4,5] 解释&#xff1a;链表只有一个中间结…

瑞云渲染专访奥斯卡提名制片人兼女性动画人协会副主席Jinko Gotoh!

都说CG行业男多女少&#xff0c;女同胞都是珍稀资源&#xff0c;但艺术领域怎么可以没有女性视角呢&#xff1f;本期推文我们邀请到了动画行业资深大佬后藤纯子Jinko Gotoh&#xff0c;作为奥斯卡提名制片人兼女性动画人协会副主席&#xff0c;后藤纯子始终以多元文化倡导者与创…

道路运输安全员考试:备考中的心理调适与策略

备考道路运输安全员考试&#xff0c;心理调适同样重要。考试压力往往会影响考生的学习效率和考试发挥。​ 首先&#xff0c;要正确认识考试压力。适度的压力可以激发学习动力&#xff0c;但过度的压力则会适得其反。当感到压力过大时&#xff0c;要学会自我调节。可以通过运动…

卡尔曼滤波算法从理论到实践:在STM32中的嵌入式实现

摘要&#xff1a;卡尔曼滤波&#xff08;Kalman Filter&#xff09;是传感器数据融合领域的经典算法&#xff0c;在姿态解算、导航定位等嵌入式场景中广泛应用。本文将从公式推导、代码实现、参数调试三个维度深入解析卡尔曼滤波&#xff0c;并给出基于STM32硬件的完整工程案例…

【Linux】:线程池

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家带来线程池相关的知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门到精通 数据结构…

SpringMVC (一)基础

目录 SpringMVC 一 简单使用 1 新建模块选择指定参数 2 创建实现类 3 将项目启动 4 运行结果&#xff1a;在浏览器当中响应执行 二 RequestMapping 三 请求限定 SpringMVC SpringMVC是Spring的web模块&#xff0c;用来开发Web应用&#xff0c;SpringMVC应用最终作为B/…

tomcat应用的作用以及安装,以及tomcat软件的开机自启动。

一.tomcat介绍 1.作用 tomcat是一款用来部署网站服务器的一款软件。 动态网站主流语言&#xff1a; PHP, lamp/lnmp平台 Java语言&#xff0c;运行在tomcat平台。【只要这个网站或者软件是Java语言写的&#xff0c;我们都可以在tomcat平台上去运行这个java程序。】 网站是…

CSDN博客:Markdown编辑语法教程总结教程(下)

❤个人主页&#xff1a;折枝寄北的博客 Markdown编辑语法教程总结 前言1. LaTex数学公式2. 插入不同类别的图2.1 插入甘特图2.2 插入UML图2.3 插入Mermaid流程图2.4 插入Flowchart流程图2.5 插入classDiagram类图 3. CSDN快捷键4. 字体相关设置4.1 字体样式改变4.2 字体大小改变…

AI模型的构建过程是怎样的(下)

你好,我是舒旻。 上节课,我们讲了一个模型构建的前 2 个环节,模型设计和特征工程。今天,我们继续来讲模型构建的其他 3 个环节,说说模型训练、模型验证和模型融合中,算法工程师的具体工作内容,以及 AI 产品经理需要掌握的重点。 模型训练 模型训练是通过不断训练、验证…

K邻近算法

K邻近算法 1 算法介绍 1.1 什么是K-NN K-NN&#xff08;K Near Neighbor&#xff09;&#xff1a;k个最近的邻居&#xff0c;即每个样本都可以用它最接近的k个邻居来代表。K-NN算法属于监督学习方式的分类算法&#xff0c;即计算某给点到每个点的距离作为相似度的反馈。简单…