[英]What determines if a field will be in the ViewData.ModelState Dictionary?
[英]How do I get just the exceptions from ViewData.ModelState with Linq?
我编写了以下代码来提取异常以及引用来自 ASP.Net MVC 中的 ViewData.Modelstate 属性的属性的字符串键。 我认为应该可以使用 Linq 表达式来做到这一点,但这完全让我感到困惑。
var exceptions = new Dictionary<string, Exception>();
foreach (KeyValuePair<string, ModelState> propertyErrorsPair in ViewData.ModelState)
{
foreach (var error in propertyErrorsPair.Value.Errors)
{
if (error.Exception != null)
{
exceptions.Add(propertyErrorsPair.Key, error.Exception);
}
}
}
那么有没有这样做的 Linq 方式? 我猜这可能与 SelectMany 有关,但正如我所说,我无法完全弄清楚如何实现这一目标。
谢谢
这是等效的 LINQ 表达式:
var result = ViewData.ModelState.SelectMany(x => x.Value.Errors
.Where(error => error.Exception != null)
.Select(error => new KeyValuePair<string, Exception>(x.Key, error.Exception)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.