C# Linq Except 方法
使用 Except() 方法获取两个数组之间的差异。
以下是两个数组。
int[] arr = { 9, 12, 15, 20, 35, 40, 55, 67, 88, 92 };
int[] arr2 = { 20, 35 };要获取差异,请使用返回第一个列表但第二个列表中没有元素的 Except() 方法。
arr.AsQueryable().Except(arr2);
以下是完整的示例。
示例
using System;
using System.Linq;
using System.Collections.Generic;
class Program {
static void Main() {
int[] arr = { 5, 10, 15, 20, 35, 40 };
int[] except = { 20, 35 };
Console.WriteLine("Initial List...");
foreach(int ele in arr)
Console.WriteLine(ele);
IEnumerable<int> res = arr.AsQueryable().Except(except);
Console.WriteLine("New List...");
foreach (int a in res)
Console.WriteLine(a);
}
}输出
Initial List... 5 10 15 20 35 40 New List... 5 10 15 40
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP