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


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

集运算符

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

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

原理

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

实战示例

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

1. Union

并集运算符用于合并两个集合,并去除重复的元素。
在这里插入图片描述

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

交集运算符用于返回两个集合中都存在的元素。
在这里插入图片描述

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

差集运算符用于返回第一个集合中存在,而第二个集合中不存在的元素。
在这里插入图片描述

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

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

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

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

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 方法的行为。 返回的序列包含输入序列的唯一元素

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白话Learning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值