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

相关推荐
wheeldown3 分钟前
【Linux】Linux 进程通信:System V 共享内存(最快方案)C++ 封装实战 + 通信案例,4 类经典 Bug 快速修复
linux·运维·服务器·开发语言
小年糕是糕手12 分钟前
【数据结构】双向链表“0”基础知识讲解 + 实战演练
c语言·开发语言·数据结构·c++·学习·算法·链表
将车24418 分钟前
C++实现二叉树搜索树
开发语言·数据结构·c++·笔记·学习
梵得儿SHI34 分钟前
Java 反射机制核心类详解:Class、Constructor、Method、Field
java·开发语言·反射·class·constructor·java反射·java反射机制
hbqjzx1 小时前
记录一个自动学习的脚本开发过程
开发语言·javascript·学习
Sirens.1 小时前
Java核心概念:抽象类、接口、Object类深度剖析
java·开发语言·github
程序员阿鹏2 小时前
49.字母异位词分组
java·开发语言·leetcode
Hello.Reader2 小时前
Flink Data Source 理论与实践架构、时序一致性、容错恢复、吞吐建模与实现模式
架构·flink·linq
L X..2 小时前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
Yurko132 小时前
【C语言】基本语法结构(上篇)
c语言·开发语言·学习