tags:

views:

108

answers:

2

I apologise in advance if this has been answered before; it seems so fundamental to me yet I haven't turned up the answer in any of my searches.

I have a list of string tuples, say (P1,P2)

I'd like to know if there's a LINQ statement where I could group by P1 (in ascending order), and have that group contain all the P2 values for the group (in descending order).

For input: ("A","B"), ("A","C"), ("D","B") I'd like to get two groups: "A" and "D" (in that order, every time) where group "A" contains "C" and "B" (in that order, every time) and group "D" contains, well, "B".

Is this possible with the built-in LINQ classes or do I need to iterate the groups and sort them myself? Sounds like hard work~

+10  A: 

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.OrderBy(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.OrderBy(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.OrderBy(x => x))
    {
        Console.WriteLine("  {0}", value);
    }
}
Jon Skeet
Awesome. I'm going with the answer in the first (and second) panel(s). I was trying something similar to your third panel and it frustrated me that I had to declare the order in two different places - where the data was sourced and where it was used. I didn't think to put the OrderBy into the select. Also, I have no experience of the .NET 4.0 Tuple - all in due time. :-)
Jono
+2  A: 
List<Tuple<string, string>> tuples = //
var grouped = tuples.GroupBy(t => t.First)
    .OrderBy(grp => grp.Key)
    .Select(grp => new { Key = grp.Key, Items = grp.OrderBy(t => t.Second) });
Lee