Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I return two lists of objects from one Controller Action?

I am having two different list coming from same object. I want to get those two at same time as separate list or join those two same list when returning JSON object.

Here is my code.

List<User> userEntity = users.Where(s => s.Id == Id).ToList();

var GetUserNames = userEntity.SelectMany(s => s.Names.Select(u =>
    new
    {
        Name = u.Name,
        Id = u.Id
    })).ToList();

var GetProfile = userEntity.SelectMany(s => s.Profile.Select(u =>
    new
    {
        Name = u.Name,
        Id = u.Id
    })).ToList();

return Json(GetUserNames, JsonRequestBehavior.AllowGet);
like image 696
Chatra Avatar asked Dec 19 '22 06:12

Chatra


1 Answers

I would prefer to do this differently: the returned items are different types. Rather than returning a bare JSON list with a type discriminator, return a multiproperty JSON object:

return Json(new 
{ 
    Names = GetUserNames, 
    Profiles = GetProfile 
}, JsonRequestBehavior.AllowGet);

Your returned JSON will have the {Name, Id} objects separated into their types, in sketch form:

{ 
  Names: [
   {Name:"UserName", Id:"3"}, 
   {Name:"OtherUser", Id: "4"}
  ],
  Profiles: [
   {Name:"Normal", Id:"1"}, 
   {Name:"Admin", Id: "99"}
  ]
}

In most JSON.Net client side parsing scenarios (i.e. consuming a WebAPI from a WPF smart client), you automatic mapping (such as from RestSharp) would allow you to deserialize this into a class of the form

public class NameId
{
     public int Id {get; set;}
     public string Name {get; set;}
}

public class UserNamesResponse
{
     public List<NameId> Names {get; set;}
     public List<NameId> Profiles {get; set;}
}

This may be more convenient and clearer to work with than an interleaved list which must be filtered into separate lists for binding anyway.... (or fed through filtering type converters at a performance penalty in the UI binding layer...)

like image 101
Tetsujin no Oni Avatar answered Dec 24 '22 00:12

Tetsujin no Oni