Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get linq result as string array?

Tags:

json

arrays

linq

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?

like image 936
twk Avatar asked Aug 08 '09 12:08

twk


1 Answers

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 });
like image 57
mmx Avatar answered Oct 16 '22 05:10

mmx