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

LINQ except方法

本文概述

在LINQ中, 使用Except方法或运算符仅返回第一个集合中的元素, 而第二个集合中不存在这些元素。

这是LINQ Except方法的图形表示。

LINQ except方法

如上图所示, 它将从第一个集合中返回元素, 而第二个集合中不存在该元素。

LINQ Except方法的语法

使用LINQ Except方法从第一个列表获取元素的语法, 该元素在第二个列表中不存在。

var result = arr1.Except(arr2);

根据以上语法, 我们正在比较两个列表” arr1″, ” arr2″, 并使用Except()方法获取元素。

LINQ except方法的示例

这是LINQ Except方法的示例, 该方法从第一个集合中获取元素。

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 an array 'a' and 'b' type of string having the values
            string[] a = { "Rohini", "Suresh", "Sateesh", "Praveen" };
            string[] b = { "Madhav", "Sushmitha", "Sateesh", "Praveen" };
//Except method is used to return the value which does not exist in the second list 
            var result = a.Except(b);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
                Console.ReadLine();
        }
    }
}

输出

LINQ except方法

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

评论 抢沙发

评论前必须登录!