掌握 LINQ:通过示例解释 C# 中强大的 LINQ的集运算

文章目录


在C#中,LINQ(Language Integrated Query)提供了丰富的集合操作功能,使得对集合数据进行查询、过滤、排序等操作变得非常便捷和灵活。下面我将详细介绍几种常见的集运算,并给出示例代码,帮助你更好地理解和应用。

集运算符

LINQ提供了以下几种集运算符:

  • Union: 返回两个集合的并集,去除重复元素。
  • Intersect: 返回两个集合的交集。
  • Except: 返回第一个集合中不在第二个集合中的元素。
  • ExceptWith: 返回两个集合中不重复的元素,即第一个集合中不在第二个集合中的元素和第二个集合中不在第一个集合中的元素。
  • Concat: 连接两个集合,返回一个包含两个集合所有元素的集合。

原理

集运算符基于LINQ的查询表达式和Enumerable类提供的方法。当你使用集运算符时,LINQ会构建一个查询表达式树(Query Expression Tree),然后通过Enumerable类中的扩展方法来执行这些查询。

实战示例

下面通过具体的示例来展示如何使用LINQ的集运算符。

1. Union

并集运算符用于合并两个集合,并去除重复的元素。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list1 = new List<int>() { 1, 2, 3 };
        List<int> list2 = new List<int>() { 3, 4, 5 };

        var result = list1.Union(list2).ToList();

        // 输出结果: 1, 2, 3, 4, 5
        foreach (var item in result)
        {
            Console.WriteLine(item);
        }
    }
}

2. Intersect

交集运算符用于返回两个集合中都存在的元素。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list1 = new List<int>() { 1, 2, 3 };
        List<int> list2 = new List<int>() { 3, 4, 5 };

        var result = list1.Intersect(list2).ToList();

        // 输出结果: 3
        foreach (var item in result)
        {
            Console.WriteLine(item);
        }
    }
}

3. Except

差集运算符用于返回第一个集合中存在,而第二个集合中不存在的元素。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list1 = new List<int>() { 1, 2, 3 };
        List<int> list2 = new List<int>() { 3, 4, 5 };

        var result = list1.Except(list2).ToList();

        // 输出结果: 1, 2
        foreach (var item in result)
        {
            Console.WriteLine(item);
        }
    }
}

4. ExceptWith

对称差集运算符用于返回两个集合中不重复的元素。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list1 = new List<int>() { 1, 2, 3 };
        List<int> list2 = new List<int>() { 3, 4, 5 };

        var result = list1.ExceptWith(list2).ToList();

        // 输出结果: 1, 2, 4, 5
        foreach (var item in result)
        {
            Console.WriteLine(item);
        }
    }
}

5. Concat

连接运算符用于连接两个集合,返回一个包含两个集合所有元素的集合。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list1 = new List<int>() { 1, 2, 3 };
        List<int> list2 = new List<int>() { 3, 4, 5 };

        var result = list1.Concat(list2).ToList();

        // 输出结果: 1, 2, 3, 3, 4, 5
        foreach (var item in result)
        {
            Console.WriteLine(item);
        }
    }
}

6. Distinct

删除集合中的重复值。
字符序列上 Enumerable.Distinct 方法的行为。 返回的序列包含输入序列的唯一元素

csharp 复制代码
string[] planets = { "Mercury", "Venus", "Venus", "Earth", "Mars", "Earth" };
IEnumerable<string> query = from planet in planets.Distinct()
select planet;
foreach (var str in query)
{
Console.WriteLine(str);
}
/* 输出:
*
* Mercury
* Venus
* Earth
* Mars
*/

注意事项

  1. 使用集运算符时,集合类型必须相兼容,即它们必须能够进行隐式类型转换或者显式指定类型。
  2. 集运算符通常用于IEnumerable或IQueryable类型的集合。
  3. 集运算符不会改变原始集合,而是返回一个新的集合。

总结

LINQ的集运算符提供了一种简洁明了的方式来对集合进行基本的集合运算。通过这些运算,可以很容易地在程序中实现集合的合并、交集、差集等操作,极大地简化了集合操作的复杂性。在实际开发中,合理使用这些运算符可以提高代码的可读性和可维护性。

相关推荐
小白狮ww8 分钟前
Retinex 算法 + MATLAB 软件,高效率完成图像去雾处理
开发语言·人工智能·算法·matlab·自然语言处理·图像识别·去雾处理
cwtlw20 分钟前
java基础知识面试题总结
java·开发语言·学习·面试
西元.26 分钟前
多线程循环打印
java·开发语言·jvm
高林雨露26 分钟前
Kotlin 基础语法解析
android·开发语言·kotlin
ml1301852887433 分钟前
DeepSeek 助力心理医生小程序赋能!心理咨询小程序 线上咨询平台搭建
java·开发语言·小程序
不辉放弃33 分钟前
零基础讲解pandas
开发语言·python
tangweiguo030519871 小时前
(Kotlin)Android 高效底部导航方案:基于预定义 Menu 和 ViewPager2 的 Fragment 动态绑定实现
android·开发语言·kotlin
ChiaWei Lee1 小时前
【C语言】深入理解指针(三):C语言中的高级指针应用
c语言·开发语言
最后一个bug1 小时前
教你快速理解linux中的NUMA节点探测是干什么用的?
linux·c语言·开发语言·arm开发·嵌入式硬件
鲤籽鲲1 小时前
C# System.Net.IPEndPoint 使用详解
网络·microsoft·c#·.net