ModelState creates a list string with the key + ErrorMessage. (LINQ)

I want to get something like this:

'myKey: errorMessage'

Now I have a list with all ModelState errors:

List<String> modelStateErrors2 = ModelState.Keys.SelectMany(key => this.ModelState[key].Errors).Select(x => x.ErrorMessage).ToList(); 

But you need to add the key to the beginning of the line.

Maybe?

+4
source share
1 answer

You need to move Select() inside SelectMany() so that it can close above key :

 ModelState.Keys.SelectMany(key => this.ModelState[key].Errors.Select(x => key + ": " + x.ErrorMessage)); 

This would be simpler as an understanding of the request:

 from kvp in ModelState from e in kvp.Value.Errors select kvp.Key + ": " + e.ErrorMessage 
+9
source

Source: https://habr.com/ru/post/1498424/


All Articles