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() });
Source: stackoverflow.com
Related Query
- How do i convert this linq code to inline sql
- How to convert this recursive code to Linq
- Linq - how to convert this code to linq
- How can i convert this code snippet into LINQ format?
- How do I convert this looped code to a single LINQ implementation?
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How does this linq code that splits a sequence work?
- How can I combine this code into one or two LINQ queries?
- How can I further simplify this piece of LINQ code
- How to render this map-reduce javascript code to an equivalent LINQ Select-Aggregate?
- How to convert this Linq query syntax to method syntax?
- How to convert this foreach loop into Linq code?
- How do I convert this SQL query to LINQ to SQL
- How to convert this linq query to lambda?
- How to convert this SQL query to LINQ or Lambda expression?
- How might I convert this SQL code to LINQ?
- How do I convert this to using Linq to dataset?
- Linq Puzzle: How do I convert this foreach to linq syntax?
- How to convert this LINQ query syntax that connects two objects to method syntax?
- How to convert this code to LINQ?
- How can I convert this SQL to LINQ
- How to convert this complex SQL Query with Subqueries into LINQ
- How to write this code using the Linq Extension Method-Syntax?
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- How can I refactor this code for LINQ filtering?
- How to convert this SQL to LINQ in C#?
- How to convert this to LINQ
- How do I convert this tSQL statement to LINQ using group by in a sub query
- How to convert this LEFT JOIN Linq Query to Lambda expression
- How can I convert this SQL to VB.NET LINQ
More Query from same tag
- Convert List<List<int>> to double[][]
- LINQ getting an item <t> from a collection
- remove a duplicate element(with specific value) from xml using linq
- LINQ orderBy with possible null values
- Search specific node value in XDocument
- MVC Linq query returning an inherited Entity's ID, but the Entity is null. How do I .Include() TPH members?
- Adding a second Join with LINQ
- How to use LINQ to group by one field, sort within that grouping, and sum values for that combination?
- Empty class to use with LINQtoCSV - vb.net
- Explain performance differences between different LINQ statements
- C# Entity Framework return list from nested object
- How do I flatten an array of arrays?
- Finding location in list,
- Forcing XDocument.ToString() to include the closing tag when there is no data
- In group by elements, select value different from null in other column
- Trying to follow the example for ToTraceString(), but it fails. Why?
- Get specific object in list in Entity Framework
- Linq to XML complete Child Element XL structure
- LINQ Any failing?
- How would I get the corresponding Key for the maximum Value in a Dictionary(Of SomeEnum, Integer) using LINQ?
- Can I use LINQ to check if objects in a list have a unique ID?
- What advantages and drawbacks does Enumerable.SequenceEqual(list1, list2) has against list1.All(list2.Contains)?
- Linq Join, group by multiple field
- LINQ Simple Query takes over 30s
- Add items to list from linq var
- Compare two list if match found update first list property
- LINQ to XML - How to use XDocument the right way
- Linq remove only one item if there are duplicate
- c# Datatable force DataType when using Adapter fill
- Building a LINQ lambda expression that can output enums, decimals, and strings