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?
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
IsBasketBallis 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 (includingMaterial), but you can use someDefaultMaterialvalue for elements in the basket – which means that the material won’t be actually used for grouping: