Nope, it's not hard - you just need to keep track of whether you're looking at a group or elements within the group. Here's a sample query:
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1 into g
select new { Group = g.Key,
Elements = g.OrderByDescending(p2 => p2) };
Here's a complete example (avoiding .NET 4's Tuple
type just for simplicity if you're using .NET 3.5):
using System;
using System.Collections.Generic;
using System.Linq;
public class MyTuple
{
public string P1 { get; set; }
public string P2 { get; set; }
}
class Test
{
static void Main()
{
List<MyTuple> tuples = new List<MyTuple>
{
new MyTuple { P1 = "A", P2 = "B" },
new MyTuple { P1 = "A", P2 = "C" },
new MyTuple { P1 = "D", P2 = "B" },
};
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1 into g
select new { Group = g.Key,
Elements = g.OrderByDescending(p2 => p2) };
foreach (var group in query)
{
Console.WriteLine("{0}:", group.Group);
foreach (var value in group.Elements)
{
Console.WriteLine(" {0}", value);
}
}
}
}
Note that it can be slightly simpler if you're happy to do the ordering on a "need to know" basis:
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1;
foreach (var group in query)
{
Console.WriteLine("{0}:", group.Key);
foreach (var value in group.OrderByDescending(x => x))
{
Console.WriteLine(" {0}", value);
}
}