【C#】working with sets and bags using LINQ

此代码示例展示了C#中使用LINQ进行集合操作,如去重(Distinct)、合并(Union)、连接(Concat)、求交集(Intersect)、求差集(Except)以及Zip操作,用于匹配两个集合的元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using static System.Console;
using System.Collections.Generic; // for IEnumerable<T> 
using System.Linq; // for LINQ extension methods

namespace LinqWithSets
{
    class Program
    {
        static void Output(IEnumerable<string> cohort,
          string description = "")//输出字符串集合
        {
            if (!string.IsNullOrEmpty(description))
            {
                WriteLine(description);
            }
            Write("  ");
            WriteLine(string.Join(", ", cohort.ToArray()));
        }

        /*
            Cohort 1
              Rachel, Gareth, Jonathan, George
            Cohort 2
              Jack, Stephen, Daniel, Jack, Jared
            Cohort 3
              Declan, Jack, Jack, Jasmine, Conor

            cohort2.Distinct():
              Jack, Stephen, Daniel, Jared

            cohort2.Union(cohort3):
              Jack, Stephen, Daniel, Jared, Declan, Jasmine, Conor

            cohort2.Concat(cohort3):
              Jack, Stephen, Daniel, Jack, Jared, Declan, Jack, Jack, Jasmine, Conor

            cohort2.Intersect(cohort3):
              Jack

            cohort2.Except(cohort3):
              Stephen, Daniel, Jared

            cohort1.Zip(cohort2):
              Rachel matched with Jack, Gareth matched with Stephen, Jonathan matched with Daniel, George matched with Jack
         */
        static void Main(string[] args)
        {
            var cohort1 = new string[]
              { "Rachel", "Gareth", "Jonathan", "George" };
            var cohort2 = new string[]
              { "Jack", "Stephen", "Daniel", "Jack", "Jared" };
            var cohort3 = new string[]
              { "Declan", "Jack", "Jack", "Jasmine", "Conor" };

            Output(cohort1, "Cohort 1");
            Output(cohort2, "Cohort 2");
            Output(cohort3, "Cohort 3");
            WriteLine();
            Output(cohort2.Distinct(), "cohort2.Distinct():");//通过使用默认的相等比较器比较值,从序列中返回不同的元素。
            WriteLine();
            Output(cohort2.Union(cohort3), "cohort2.Union(cohort3):");//使用默认的相等比较器生成两个序列的集合并集
            WriteLine();
            Output(cohort2.Concat(cohort3), "cohort2.Concat(cohort3):");//连接两个序列。
            WriteLine();
            Output(cohort2.Intersect(cohort3), "cohort2.Intersect(cohort3):");//通过使用默认相等比较器比较值来生成两个序列的集合交集。
            WriteLine();
            Output(cohort2.Except(cohort3), "cohort2.Except(cohort3):");//通过使用默认的相等比较器比较值来产生两个序列的集合差异。
            WriteLine();
            //将指定函数应用于两个序列的相应元素,产生结果序列。
            Output(cohort1.Zip(cohort2, (c1, c2) => $"{c1} matched with {c2}"),
              "cohort1.Zip(cohort2):");
            ReadLine();

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值