LoginSignup
10
9

More than 5 years have passed since last update.

Dictionary.FirstOrDefault() で値が取得できたか判別する方法

Posted at

LINQで少しつまづいたのでメモ。

以下のように、default(Dictionary) と等しいかどうか比較することで判別できます。

FirstOrDefault.cs
// ディクショナリから、条件に合致する値のペアを取得する
var matchedPair = myDict.FirstOrDefault(item => item.Key.Match(name).Success);

// 取得できなかったら中断
if (matchedPair.Equals(default(Dictionary<Regex,string>)))
    return "";

//if (matchedPair == null) // ← これだとエラーになる
//    return "";
10
9
3

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
10
9