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 {
static void Main() {
List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };
// distict elements from the list
IEnumerable<int> res = points.AsQueryable().Distinct();
foreach (int a in res) {
Console.WriteLine(a);
}
}
} 出力
5 10 20 30 40 50 60 70
-
シーケンスから個別の要素を取得するC#プログラム
シーケンスを設定し、要素を追加します。 List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 }; Distinct()メソッドを使用して、上記のリストから個別の要素を取得します。 IEnumerable<int> res = ID.AsQueryable().Distinct(); 完全なコードを見てみましょう。 例 using System; using System.Linq; using System.Collections.Generic; class Dem
-
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