2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C#】Listから指定条件のデータを取得する方法

Posted at

結論

Where()を使います。

例(Listの要素からApexのみを取得する)
List<string> dataList = new() { "Apex", "Valorant", "Fortnite", "PUBG", "APEX", "PSO2NGS", "GTA5"};

const string searchString = "Apex";

//ListからApexという文字列のみを取得する!
var onlyApexList = dataList.Where(x => x == searchString).ToList();

//Apexという文字列の要素がList内に何個あったかを表示!
Console.WriteLine($"{searchString}{onlyApexList.Count}個あるぞ!!");
//出力:Apexは1個あるぞ!

解説

結論では"Apex"という文字列をListから取得する例を記載しました。実際にListから条件を指定してデータを取得している箇所はここです。

//ListからApexという文字列のみを取得する!
var onlyApexList = dataList.Where(x => x == searchString).ToList();

x =>ってなんや...(^ρ^)

イメージとしてはこんな処理をしてくれています
//ListからApexという文字列のみを取得する!
List<string> onlyApexList = new();

foreach (var x in dataList)
{
    //もし要素がApexという文字列だったら新しいリストに追加する!
    if (x == searchString) onlyApexList.Add(x);
}

(´ε` )ふぅん...
実際にWhere()が何をやっているか詳しく知りたい方はリファレンスソースを確認してください!

C#リファレンスソース

おまけ

Apexっていう文字列を含む要素一覧を取得したい場合

Apexを含む文字列の要素を取得
List<string> dataList = new() { "Apex", "Valorant", "Fortnite", "PUBG", "APEX", "PSO2NGS", "GTA5"};

const string searchString = "Apex";

//ListからApexという文字列を含む要素を取得する!
var onlyApexList = dataList.Where(x => x.Contains(searchString)).ToList();

//Apexという文字列の要素がList内に何個あったかを表示!
Console.WriteLine($"{searchString}{onlyApexList.Count}個あるぞ!!");
//出力:Apexは1個あるぞ!

大文字小文字関係なくApexという文字列を含む要素一覧を取得したい場合

大文字小文字関係なくApexを含む文字列の要素を取得
List<string> dataList = new() { "Apex", "Valorant", "Fortnite", "PUBG", "APEX", "PSO2NGS", "GTA5"};

const string searchString = "Apex";

//ListからApexという文字列を含む要素を取得する!
var onlyApexList = dataList.Where(x => x.Contains(searchString, StringComparison.OrdinalIgnoreCase)).ToList();

//Apexという文字列の要素がList内に何個あったかを表示!
Console.WriteLine($"{searchString}{onlyApexList.Count}個あるぞ!!");
//出力:Apexは2個あるぞ!

要素の先頭文字列がPで始まる要素一覧を取得したい場合

要素の先頭文字列がPで始まる要素一覧を取得
List<string> dataList = new() { "Apex", "Valorant", "Fortnite", "PUBG", "APEX", "PSO2NGS", "GTA5"};

const string searchString = "P";

//Listから先頭文字列がPで始まる要素を取得する!
var onlyApexList = dataList.Where(x => x.StartsWith(searchString)).ToList();

//要素の先頭文字列がPで始まる要素がList内に何個あったかを表示!
Console.WriteLine($"先頭文字列が{searchString}から始まる要素は{onlyApexList.Count}個あるぞ!!");
//出力:先頭文字列がPから始まる要素は2個あるぞ!!

要素の末尾がxで終わる要素一覧を取得したい場合

要素の末尾がxで終わる要素一覧を取得
List<string> dataList = new() { "Apex", "Valorant", "Fortnite", "PUBG", "APEX", "PSO2NGS", "GTA5"};

const string searchString = "x";

//Listから末尾がxで終わる要素を取得する!
var onlyApexList = dataList.Where(x => x.EndsWith(searchString)).ToList();

//要素の末尾がxで終わる要素がList内に何個あったかを表示!
Console.WriteLine($"末尾が{searchString}で終わる要素は{onlyApexList.Count}個あるぞ!!");
//出力:末尾がxで終わる要素は1個あるぞ!!

以上です。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?