GroupBy - Simple 2

Query
public void Linq41()
{
string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };

var wordGroups =
from w in words
group w by w[0] into g
select new { FirstLetter = g.Key, Words = g };

foreach (var g in wordGroups)
{
Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter);
foreach (var w in g.Words)
{
Console.WriteLine(w);
}
}
}
Lambda Expression
public void DataSetLinq41()
{
var words4 = testDS.Tables["Words4"].AsEnumerable();

var wordGroups = words4.GroupBy(w => w.Field("word")[0]).Select(g => new {FirstLetter = g.Key, Words = g});

foreach (var g in wordGroups)
{
Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter);
foreach (var w in g.Words)
{
Console.WriteLine(w.Field("word"));
}
}
}
Output
Words that start with the letter 'b':
blueberry
banana
Words that start with the letter 'c':
chimpanzee
cheese
Words that start with the letter 'a':
abacus
apple
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +