I'm playing around with GroupBy(...) and I realize that, although working, I prefer not to use var. Call me anal but I like to know what type I have when I have it. (Of course, I'm not an ideologue and when the syntax is making ones eyes bleed, I do use var. Still, it's nice to know how to explicitize it, should ones urge emerge.)
var grouping = array.GroupBy(element => element.DayOfWeek);
foreach (var group in grouping)
{
Console.WriteLine("Group " + group.Key);
foreach (var value in group)
Console.WriteLine(value.Start);
}
According to the intellisense, I'm generating something of type IGrouping but when I tried to type that in explicitly, weird stuff happened. I've arrived at the following.
IEnumerable<IGrouping<DayOfWeek, TimeInfo>> grouping
= array.GroupBy(element => element.DayOfWeek);
foreach (IGrouping<DayOfWeek, TimeInfo> group in grouping)
{
Console.WriteLine("Group " + group.Key);
foreach (TimeInfo value in group)
Console.WriteLine(value.Start);
}
My question is twosome. Is the below the correct and most narrow scope for the produced result? If not, how can I tight it up additionally?
If it is so, however, I'd like to know if there's a better way to approach the task. I only need to put my elements from the array into different bins with respect to day of the weeks (or any other condition in the actual production). I don't really need the key, just a way to make my 1D array into 2D array of arrays. Something like an opposite to SelectMany, so to speak. (No extensive answer needed - just a keyword to google away.)