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

相关推荐
有人说风轻的像雨39 分钟前
c#的反射和特性
c#
就叫啥也不会吧1 小时前
QT:信号映射器
开发语言·qt
医学生_R语言1 小时前
【R语言可视化】相关系数热图
开发语言·python·r语言
小画家~1 小时前
第十五:使用Air实现Go程序实时热重载
开发语言·后端·golang
鲤籽鲲2 小时前
C# SerialPort 类中清空缓存区的方法
开发语言·c#·上位机
Silber 甜2 小时前
MacOS bash&zsh 命令行自动推荐补全工具
开发语言·macos·bash
小张爱小余2 小时前
内网渗透-DLL和C语言加载木马
c语言·开发语言
CoderIsArt2 小时前
C#中3维向量的实现
开发语言·c#
假装我不帅3 小时前
C# 操作html下的css样式
css·c#·html
demonlg01123 小时前
Go 语言 net/http模块的完整方法详解及示例
开发语言·后端·http·云原生·golang