C# 程序返回两个序列之间的差异

csharpprogrammingserver side programming更新于 2025/9/14 5:06:00

设置两个序列。

double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };
double[] arr2 = { 15.6, 30.5, 50.2 };

要获取上述两个数组之间的差异,请使用 Except() 方法。

IEnumerable<double> res = arr1.AsQueryable().Except(arr2);

以下是完整代码。

示例

using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
   static void Main() {
      double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };
      double[] arr2 = { 15.6, 30.5, 50.2 };
      Console.WriteLine("初始列表...");
      foreach(double ele in arr1) {
         Console.WriteLine(ele);
      }
      IEnumerable<double> res = arr1.AsQueryable().Except(arr2);
      Console.WriteLine("新列表...");
      foreach (double a in res) {
         Console.WriteLine(a);
      }
   }
}

输出

初始列表...
10.2
15.6
23.3
30.5
50.2
新列表...
10.2
23.3

相关文章