5

I Want to obtain something like this:

' myKey: errorMessage '

Now i've a list with all the ModelState errors:

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

But need to add the key in the begin of the string.

It is possible?

1 Answer 1

12

You need to move the Select() inside the SelectMany() so it can close over the key:

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

This would be simpler as a query comprehension:

from kvp in ModelState
from e in kvp.Value.Errors
select kvp.Key + ": " + e.ErrorMessage
2
  • 1
    @EzequielLeiva: You're welcome! BTW, kvp stands for KeyValuePair.
    – SLaks
    Commented Aug 23, 2013 at 0:20
  • The LINQ query is nice! Commented Dec 2, 2013 at 21:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.