score:43

Accepted answer

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.

score:3

here's a straightforward console application that does what you want:

static void main(string[] args)
{
    list<siteinfo> sites = new list<siteinfo>()
    {
        new siteinfo() { title = "site a", type = "whatever 2" },
        new siteinfo() { title = "site b", type = "whatever 1" },
        new siteinfo() { title = "site c", type = "whatever 1" },
        new siteinfo() { title = "site d", type = "whatever 3" },
        new siteinfo() { title = "site e", type = "whatever 3" }
    };

    var sitesgroupedbytype =
        sites.groupby(s => s.type).select(g => new { type = g.key,
                                    sites = g.select(site => new
                                    {
                                          site.title,
                                          site.url
                                    })});

    foreach (var sitetypegroup in sitesgroupedbytype.orderby(g => g.type))
    {
        foreach(var site in sitetypegroup.sites)
        {
            console.writeline(string.format("type => {0}, title => {1}",
                              sitetypegroup.type, site.title));
        }
    }

    console.readkey();
}

output:

type => whatever 1, title => site b
type => whatever 1, title => site c
type => whatever 2, title => site a
type => whatever 3, title => site d
type => whatever 3, title => site e

score:5

this

var sites = new list<siteinfo>()
{
    new siteinfo(){title="1",type="a",url="http://aaaa"},
    new siteinfo(){title="2",type="b",url="http://bbbb"},
    new siteinfo(){title="3",type="a",url="http://aaaa"},
    new siteinfo(){title="4",type="b",url="http://bbb"},
};

var json = new javascriptserializer().serialize(sites.groupby(s => s.type));

would produce

[
    [{"title":"1","url":"http://aaaa","type":"a"},{"title":"3","url":"http://aaaa","type":"a"}],
    [{"title":"2","url":"http://bbbb","type":"b"},{"title":"4","url":"http://bbb","type":"b"}]
]

or

var json = new javascriptserializer().serialize(sites.groupby(s => s.type)
                                                      .todictionary(x=>x.key,x=>x));

would produce

{
    "a":[{"title":"1","url":"http://aaaa","type":"a"},{"title":"3","url":"http://aaaa","type":"a"}],
    "b":[{"title":"2","url":"http://bbbb","type":"b"},{"title":"4","url":"http://bbb","type":"b"}]
}

Related Query

More Query from same tag