我想将用户属性列表转换为字符串数组(对于json接收器),如:
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)
结果是一个名为fields的数组
[{"Id":1,"Lastname":"Doe","Firstname":"John"},{"Id":2,"Lastname":"Smith","Firstname":"Adam"},...]
但我希望它是普通字符串数组的数组,如:
[["1","Doe","John"] ["2","Smith","Adam"],...]
如何将linq结果转换为字符串数组?
解决方法
var jsonData = from user in users select new[] { user.Id.ToString(),user.Person.Firstname };
或者,您可以使用lambda语法:
var jsonData = users.Select(user => new[] { user.Id.ToString(),user.Person.Firstname });