score:2

Accepted answer

the exact translation would be:

var errors = modelstate.keys.select(k => modelstate[k].errors.first().errormessage);
return json(new { success = false, errors = errors.tolist() });

provided modelstate is a dictionary<tkey,tvalue> or similar, you could use the values directly:

var errors = modelstate.values.select(v => v.errors.first().errormessage);
return json(new { success = false, errors = errors.tolist() });

score:2

return new json(new 
   {
      success = false,
      errors = modelstate.keys.select(k => modelstate[key].errors.first().errormessage).tolist()
   });

score:3

the closes translation (which is unsafe because firstordefault() could return null in which case your code would throw a null reference exception) would be:

return json(new { success = false,
                  errors = modelstate.values
                      .select(ms => ms.errors.firstordefault().errormessage)
                      .tolist() });

you could make it a bit safer using:

return json(new { 
    success = false,
    errors =
        modelstate.values
                  .select(ms => 
                          { 
                              var error = ms.errors.firstordefault();
                              return error == null ? error.errormessage : "";
                          })
                  .tolist() });

Related Query

More Query from same tag