after thinking about this, this is what i came up with. it's more or less the same as kirk's but doesn't use a dictionary. i was aiming for a single query to do this, (select ... into) really saved me there.

var query = from item in data
            where item.rowlabel == "all"
            orderby item.value descending
            select item.columnlabel into columnorder
            join item in data on columnorder equals item.columnlabel
            group item by item.rowlabel;

var lquery = data.where(item => item.rowlabel == "all")
                 .orderbydescending(item => item.value)
                 .select(item => item.columnlabel)
                 .join(data, columnorder => columnorder,
                             item => item.columnlabel,
                             (columnorder, item) => item)
                 .groupby(item => item.rowlabel);


i believe this will do what you want:

var order = data
    .where(x => x.rowlabel == "all")
    .orderbydescending(x => x.value)
    .select((x, i) => new { x, i })
    .todictionary(x => x.x.columnlabel, x => x.i);
var result = data
    .groupby(x => x.rowlabel, (x, y) => y.orderby(z => order[z.columnlabel]));

foreach (var x in result)
    foreach (var y in x)
        console.writeline("columnlabel: {0}, rowlabel: {1}, value: {2}", y.columnlabel, y.rowlabel, y.value);                    


columnlabel: dog, rowlabel: all, value: 17 
columnlabel: cat, rowlabel: all, value: 9 
columnlabel: fish, rowlabel: all, value: 4
columnlabel: dog, rowlabel: girls, value: 2
columnlabel: cat, rowlabel: girls, value: 3
columnlabel: fish, rowlabel: girls, value: 2
columnlabel: dog, rowlabel: boys, value: 15
columnlabel: cat, rowlabel: boys, value: 6
columnlabel: fish, rowlabel: boys, value: 2

let me know if i misinterpreted your question, but the idea here is to create a dictionary order that provides the ordering for the subsequent group-by query. the other key idea is to use the resultselector of group-by to order the elements within (since those represent the columns).

Related Query

More Query from same tag