I have a simple list which is returned from my DB to my MVC API, the sample query is
select school, class, pupil from area
With that I am adding to a list
foreach (DataRow row in table.Rows)
{
var area = new Area();
area.school = row.ItemArray[0].ToString();
area.class = row.ItemArray[1].ToString();
area.pupil = row.ItemArray[2].ToString();
returnedlist.Add(area);
}
at the moment the API just returns the list
{
"School": "",
"Class": "",
"Pupil": ""
},
{
"School": "",
"Class": "",
"Pupil": ""
},
However, I would ideally like it returned nested like the following
[
{
School: "Name",
Class: [
ClassName: '',
Pupil: [
PupilName: ''},
PupilName: ''},
PupilName: '']
},
{
School: "Name",
Class: [
ClassName: '',
Pupil: [
PupilName: ''},
PupilName: ''},
PupilName: '']
},
I may have butchered that but you get the general idea.
The class for the data again is very simple
public class Area
{
public string School { get; set; }
public string Class { get; set; }
public string Pupil { get; set; }
}
So far I have tried returning a list in a list etc, but without any luck.
Any help greatly appreciated.
areaclass androw?, you could change the structure of area class.returnedlist. group by school and by class to build new list of expected object.