2

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
David James Ball
  • 903
  • 10
  • 26
  • Not really sure what problem you have here. Perhaps you just need to add `.Single()` after the `Select()`? – DavidG Oct 12 '14 at 23:42
  • Hi, I edited my question as I forgot to mention that more than one property can have the same name, so Single() would cause an exception. – David James Ball Oct 12 '14 at 23:52
  • So what do you want to happen when grouping by Colour? – DavidG Oct 12 '14 at 23:58
  • I would want to have the group keys as red, blue, yellow, black, etc and each group would contain any cars that have a property named 'Colour' with that key value as the colour value. I would have some cars belonging to more than one group, which I want. – David James Ball Oct 13 '14 at 00:01
  • That is not how "GroupBy" works - it splits set into multiple subsets (groups) based on criteria. What you seem to need is to get all "colors" and than for each color select *all* cars that have the color (or whatever other key you have). – Alexei Levenkov Oct 13 '14 at 01:22
  • 2
    Possible duplicate of [LINQ: grouping based on property in sublist](https://stackoverflow.com/questions/5258093/linq-grouping-based-on-property-in-sublist) – Heretic Monkey Oct 02 '19 at 17:51

1 Answers1

2

It seems I was making the whole thing more complicated than necessary. This answer helped me find a solution:

LINQ: grouping based on property in sublist

Community
  • 1
  • 1
David James Ball
  • 903
  • 10
  • 26