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

相关推荐
悟能不能悟几秒前
JAVA 中dao层的实体应该属于哪个层次VO,还是DTO,或者其他
java·开发语言
二狗哈4 分钟前
Cesium快速入门17:与entity和primitive交互
开发语言·前端·javascript·3d·webgl·cesium·地图可视化
chenyuhao20244 分钟前
Linux系统编程:Ext文件系统
linux·运维·服务器·开发语言·网络·c++·后端
沐知全栈开发1 小时前
C 标准库 - <locale.h>
开发语言
老毛肚1 小时前
Java两种代理模式详解
java·开发语言·代理模式
小此方1 小时前
Re:从零开始学C++(二)基础精讲·中篇:引用
开发语言·c++·底层
消失的旧时光-19431 小时前
Java 线程通信:彻底理解 wait / notify(原理 + 图解 + 实战)
java·开发语言
云中飞鸿1 小时前
wpf 类图
c#
Coder_Boy_2 小时前
【DDD领域驱动开发】基础概念和企业级项目规范入门简介
java·开发语言·人工智能·驱动开发
世洋Blog2 小时前
SiYangUnityEventSystem,一个Unity中的事件系统
观察者模式·unity·c#·游戏引擎·事件系统