I'm using the GroupByMany extension method illustrated here:
http://blogs.msdn.com/b/mitsu/archive/2007/12/22/playing-with-linq-grouping-groupbymany.aspx
This is working great, except I'm trying to group based on a list of entity properties.
An example of this would be a Car class that has a set of properties:
public class Car
{
private string Model {get; set;}
private int Year {get; set;}
private List<CarProperty> Properties { get; set; }
}
public class CarProperty
{
private string Name { get; set; }
private string Value { get; set; }
}
If I have a list of Car objects and the respective properties. The main issue is that the class can contain multiple properties with the same name:
Car1 : {"Colour", "Red"}, {"Colour", "Blue"}
Car2 : {"Colour", "Blue"}
Car3 : {"Colour", "Black"}, {"Colour", "Red"}
Car4 : {"Colour", "Yellow"}
I can create group selectors to group by 'Year' or 'Model' very easily:
c => c.Model;
c => c.Year;
The problem is I'm trying to create a group selector for one of the properties. I have tried this (to group by car colour):
c => c.Properties.Where(p => p.Name == "Colour").Select(p => p.Value));
But it's create a composite grouping key. I somehow need to flatten the list of possible grouping values to give:
Red: Car1, Car3
Blue: Car1, Car2
Black: Car3
Yellow: Car4
EDIT:
If the GroupByMany isn't the way to go, I'd appreciate any suggestions to take a list of groupings like {"Year", "Model", "Colour"} which will multi-level group the cars in the same order as the list of groups.
Year
--> Model
--> Colour