个性化阅读
专注于IT技术分析

LINQ联合方法

本文概述

在LINQ中, 使用Union方法或运算符将多个集合组合为单个集合, 并返回具有唯一元素的结果集合。

这是LINQ联合方法的图形表示。

LINQ Union()方法

Union方法将两个集合合并为一个集合, 并通过删除重复元素从集合中返回唯一元素。

LINQ联合方法的语法

这是使用union方法从多个集合中获取唯一元素的语法。

var result = count1.Union(count2);

在以上语法中, 我们将两个集合组合在一起, 并使用union方法将结果作为单个集合获得。

LINQ联合方法示例

这是使用LINQ联合方法的示例。

using System;
using System. Collections;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Programme2
    {
        static void Main(string[] args)
        {
//create array count1 and count2 of type string
            string[] count1 = { "India", "USA", "UK", "Australia" };
            string[] count2 = { "India", "Canada", "UK", "China" };
//count1.Union(count2) is used to find out the unique element from both the collection
            var result = count1.Union(count2);
//foreach loop is used to print the output conaining in the result
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
                Console.ReadLine();
        }
    }
}

在上面的示例中, 我们使用联合方法将两个集合” count1″, ” count2″组合在一起, 以从两个集合中获取唯一元素。

输出

LINQ Union()方法

赞(1)
未经允许不得转载:srcmini » LINQ联合方法

评论 抢沙发

评论前必须登录!