score:2

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);                    
    }
}

Result:

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).

score:1

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);

Related Articles