How do I avoid using nested foreach statements by using lambda or linq in this scenario? It's late and I cannot seem to figure out how to access the ErrorMessage property without having to revert to a nested foreach statement. What would be the equivalent method if the code was rewritten using wholly Lambda or Linq?
public static List<String> ExtractErrors(this ModelStateDictionary modelStateDictionary)
{
List<String> errors = new List<String>();
var modelErrorCollection = (from modelState in modelStateDictionary.Values
where modelState.Errors != null && modelState.Errors.Count > 0
select modelState.Errors).ToList();
foreach (var item in modelErrorCollection)
{
foreach (ModelError modelError in item)
{
errors.Add(modelError.ErrorMessage);
}
}
return errors;
}