次のコードがあります。
protected IEnumerable<string> GetErrorsFromModelState() {
var exceptions = ModelState.SelectMany(x => x.Value.Errors
.Select(error => error.Exception.Message));
var errors = ModelState.SelectMany(x => x.Value.Errors
.Select(error => error.ErrorMessage));
return exceptions.Union(errors);
}
次の場合に nullReference 例外を発生させないようにする方法はありますか?
error.Exception is null or if error.Exception.Message is null
どちらのケースでも問題が発生しており、両方のケースで IsNullOrEmpty チェックを使用してこれをコーディングする方法がわかりません。