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

LINQ相交方法

本文概述

在LINQ中, Intersect方法或运算符用于从两个集合中返回公共元素。

这是LINQ相交方法的图形表示。

LINQ相交方法

LINQ Intersect方法会将两个集合组合为一个集合, 并且仅返回集合中的匹配元素。

LINQ相交方法的语法

使用intersect方法从多个集合中获取匹配元素的语法。

var result = count1.Intersect(count2);

根据以上语法, 我们将两个集合组合在一起, 并使用intersect方法将结果作为单个集合。

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)
        {
//declare the two array variable count1 and count2 of the type string 
            string[] count1 = { "India", "Australia", "UK", "USA" };
            string[] count2 = { "India", "China", "UK", "China" };
/*apply the Intersect method on both of the array
 count1 and count2 and store the output in result variable*/
            var result = count1.Intersect(count2);
/*foreach loop will iterate over all the element of 
the variable item which store the output of the result variable*/ 
            foreach (var item in result)
            {
/*Console.WriteLine(item) print 
all element store in the item variable.*/
                Console.WriteLine(item);
            }
                Console.ReadLine();
        }
    }
}

输出

LINQ相交方法

赞(0)
未经允许不得转载:srcmini » LINQ相交方法

评论 抢沙发

评论前必须登录!