views:

34

answers:

1

I'm trying to subgroup a group using linq. This is proving to be more difficult than I thought. So far I took a quick and dirty approach but it's not as efficient as I would like. Can these two linq statements be simplified into one?:

        var basketBalls = from Ball ball in Balls
                        where ball.IsBasketBall())
                        group ball by new { Color = ball.Color, IsBasketBall= ball.IsBasketBall(), Size = ball.Size } into Group
                        where Group.Count() > 0
                        select Group;

        var nonBasketBalls = from Ball ball in Balls
                        where !ball.IsBasketBall())
                        group ball by new { Color = ball.Color, IsBasketBall= ball.IsBasketBall(), Size = ball.Size, Material = ball.Material } into Group
                        where Group.Count() > 0
                        select Group;

Here's what the two statements are trying to do in plain English. Find all balls that are basketballs and group them by color and size. If they aren't basketballs then group them by color, size and material. Is it possible to do this in a succinct linq statement?

A: 

The key issue that's making this difficult is that the types of the two results you get are different, because the two queries use different keys - and that means the result type is different.

However, you could group balls into two groups, based on whether IsBasketBall is true or not. Then you can perform the nested grouping, but the type of keys must be the same. You can achieve this by using the "larger" type (including Material), but you can use some DefaultMaterial value for elements in the basket - which means that the material won't be actually used for grouping:

var basketBalls = 
   from Ball ball in Balls 
   group ball by ball.IsBasketBall() into g
   select new {
     IsBasketBall = g.Key,
     Elements = from b in g 
                group b by new { 
                  Color = ball.Color, IsBasketBall= g.Key, Size = ball.Size, 
                  Material = g.Key ? DefaultMaterial : ball.Material }
   }
Tomas Petricek
Thanks a bunch. This questions should have been worded as conditional grouping. Can I also do this too, it seems a bit more concise: var basketBalls = from Ball ball in Balls group ball by new { Color = ball.Color, IsBasketBall= ball.IsBasketBall(), Size = ball.Size, Material = ball.IsBasketBall() ? 0 : ball.Material}
Joel Rodgers
@joe: Yes, that should be possible - it's a good point actually. That would make the code more concise (you'd only flatten the hierarchy, but that's probably fine).
Tomas Petricek