I have two separate domain model classes for "App" and "AgeGroup". The App Class is containing few basic integer and strings properties and so does the AgeGroup class.
What I'm trying to achieve is JSON output of all the apps by AppOrder with their properties, nested in their associated AgeGroups that are ordered by their GroupOrder property
Required Example JSON Output Structure
"Looped List of Age Groups, order by GroupOrder"
"Looped List of Apps, order by App Order"
First Age Group
Foo App Name
Foo App Icon
Foo App Store URL
Bar App Name
Bar App Icon
Bar App Store URL
Second Age Group
Tur App Name
Tur App Icon
Tur App Store URL
Puk App Name
Puk App Icon
Puk App Store URL
and so on...
My Approach so far:
Here is what I have in my "App.cs" Class
public int Id { get; set; }
public string Name { get; set; }
public string StoreURL { get; set; }
public int AppOrder { get; set; }
public string AppIcon { get; set; }
public AgeGroup AgeGroup { get; set; } // Linked to AgeGroup Class
public int AgeGroupId { get; set; }
In the "AgeGroup.cs" Class, I have following
public int Id { get; set; }
public int GroupOrder { get; set; }
public string Name { get; set; }
and in the AppsController.cs
[HttpGet]
[Route("api/groups")]
public IHttpActionResult AppsByGroup ()
{
var apps = _context.Apps
.OrderBy(a => a.AppOrder)
.Select(a => new
{
a.Name,
a.StoreURL,
a.AppIcon,
AgeGroup = a.AgeGroup.Name
}).GroupBy(a => a.AgeGroup);
return Json(apps);
}
It gives me correct output but without AgeGroup Names.
[
[ // Here I wanted the AgeGroup Name from the AgeGroup Table (Group A)
{
"Name": "First App for Group A",
"StoreURL": "some string",
"AppIcon": "icon string",
"AgeGroup": "Group A"
},
{
"Name": "2nd App Group A",
"StoreURL": "aslfdj",
"AppIcon": "asljf",
"AgeGroup": "Group A"
},
{
"Name": "3rd App Group A",
"StoreURL": "aslfdj",
"AppIcon": "alsfdj",
"AgeGroup": "Group A"
}
],
[ // Here I wanted the AgeGroup Name from the AgeGroup Table (Group B)
{
"Name": "1st App GroupB",
"StoreURL": "alsfdj",
"AppIcon": "alsdjf",
"AgeGroup": "Group B"
},
//and so on...
not working? Can you share some sample data and expected output? What output you are getting with current code?.GroupBy()linq query (not nested loops in a view) and a view model to represent that data. Are you wanting a method that returns aJsonResult, and how are you using that in your view (e.g. making an ajax call)?App 1 for Group XYZ