I'm new to linq and making myself use it more and more to get familiar. I just replaced a bunch of ugly dictionary-based grouping and counting code with the following linq statement:
using System;
using System.Linq;
namespace WindowsForms_TestBench
{
static class Program
{
[STAThread]
static void Main()
{
string[] numbers = new string[] { "1", "2", "1", "1", "3", "2", "1" };
var groupedNumbers =
from number in numbers
group number by number into grouped
select new { Number = grouped.Key, Count = grouped.Count() };
foreach (var groupedNumber in groupedNumbers)
Console.WriteLine("{0}({1})", groupedNumber.Number, groupedNumber.Count);
}
}
}
Pretty straightforward what's it's doing but I'm just curious if anyone spots a more efficient or, in your opinion, "better" way to accomplish the same output.