メソッドを除くC#Linq
Except()メソッドを使用して2つの配列の違いを取得します。
以下は2つのアレイです。
int[] arr = { 9, 12, 15, 20, 35, 40, 55, 67, 88, 92 };
int[] arr2 = { 20, 35 }; 違いを得るには、2番目のリストの要素を除いて最初のリストを返す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
-
C#LinqWhereメソッド
Whereメソッドは、述語に基づいて値の配列をフィルタリングします。 ここで、述語は70を超える要素をチェックしています。 Where((n, index) => n >= 70); 例 using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { int[] arr = { 10, 30, 20, 15, 90, 85, 40, 75 }; &n
-
C#Linq Distinct()メソッド
個別の要素を取得するには、Distinct()メソッドを使用します。 以下は、重複する要素を含むリストです。 List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 }; 次に、個別の要素を取得します- points.AsQueryable().Distinct(); 例全体を見てみましょう- 例 using System; using System.Linq; using System.Collections.Generic; class Demo { &nbs