It sounds like you want something like:
// No need to sort sites first
var grouped = sites.OrderBy(x => x.Type)
.GroupBy(x => x.Type);
Then just serialize grouped
. However, I don’t know quite what an IGrouping
will look like in JSON… and the type will be present in each case. You may want something like:
var grouped = sites.OrderBy(x => x.Type)
.GroupBy(x => x.Type)
.Select(g => new { Type = g.Key,
Sites = g.Select(site => new {
site.Title,
site.URL
} });
I think that would give you a nicer JSON structure.