7

I would like to convert a list of User's properties into strings array (for json receiver) like:

List<User> users = <..list of users from db...>

var jsonData = (
   from user in users
   select new { user.Id, user.Person.Lastname, user.Person.Firstname });

return Json(jsonData)

The result is an array named fields

[{"Id":1,"Lastname":"Doe","Firstname":"John"},{"Id":2,"Lastname":"Smith","Firstname":"Adam"},...]

but I would like it to be the array of arrays of plain strings like:

[["1","Doe","John"]
 ["2","Smith","Adam"], ...]

How to cast linq result to strings array?

2 Answers 2

18
var jsonData = from user in users
               select new[] { user.Id.ToString(),
                              user.Person.Lastname,
                              user.Person.Firstname };

Alternatively, you can use the lambda syntax:

var jsonData = users.Select(user => new[] { user.Id.ToString(),
                                            user.Person.Lastname,
                                            user.Person.Firstname });
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, both your proposals work well :) Anyway, I was trying such a strings array cast before, but finished with some strange index errors. Same was with your code, then I realized that my data source were returning IQueryable object which was the problem - users.ToList() solved this. Anyway, your answer was helpful to get this. Thanks.
FYI, this exact solution didn't work for me since I had numeric types mixed in. (I got an error saying that the compiler couldn't determine the type of the array.) But I used "new object[]" and this solved my problem.
@Mike: Yes, that's exactly why I've called .ToString() on user.Id which I assumed to be an integer. For new[] to work, the compiler should be able to infer the type of the array from the objects passed to it. If their types are not compatible, the compiler is going to complain.
good point; I missed that. In my case, I had to pass the data into a DataSet, and I wanted it in its original format (not a string). So I just wanted to make sure people were aware that "select new object[] { ... }" was an option.
i get this error when i try to use the ToString() The array type 'System.Object[]' cannot be initialized in a query result. Consider using 'System.Collections.Generic.List1[System.Object]' instead.`
0

I was using an IQueryable as the starting point of creating an array of values, not a List<>, but in either case you can serialise an anonymous object array, instead of a string array, to get the same result without casting values to strings.:

e.g.

var jsonData = users.Select(user => new object[] { 
                    user.Id,
                    user.Person.Lastname,
                    user.Person.Firstname 
               });

In my instance I was also using the result client-side with jQuery dataTable, so the result needed to be wrapped in another anonymous object (with the property name aadata).

e.g.

return Json(new { aaData = jsonData }, JsonRequestbehavior.AllowGet);

Hopefully this will help others that find this question looking for dataTable specific version of this problem (as I was).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.