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?