On Aug 20, 4:46*pm, Mike P <mike.p...@gmail.comwrote:
In this LINQ example, how do you get at the values within the groupings?
string[] anagrams = {"from", "salt", "earn ", "last ", "near ", "form
"};
* * * * var orderGroups = anagrams.GroupBy(
* * * * * * * * * * w =w.Trim(),
* * * * * * * * * * new AnagramEqualityComparer());
GroupBy returns an IGrouping (IIRC) which has a Key property and
implements IEnumerable<Tfor the appropriate T - so you typically
just use "foreach" over it.
Here's an example:
using System;
using System.Collections.Generic;
using System.Linq;
class Test
{
static void Main()
{
string[] strings = { "These", "strings", "will",
"be", "grouped", "by", "length", "and", "then",
"displayed", "in", "their", "groups" };
var groups = strings.GroupBy(x =x.Length);
foreach (var group in groups)
{
Console.WriteLine("Words of length: {0}", group.Key);
foreach (var word in group)
{
Console.WriteLine(" {0}", word);
}
}
}
}
Jon