1

I would like to do a group by on two properties who at 95% of the time will be the same, but sometimes it may not by

class Game { string AwayTeamPoolId; // 122 string HomeTeamPoolId; // 144 } 

I would like to group all games from the same pool from a list of games. As I mentioned above most of the time this value will be the same for both away/home teams. However there might be a cross pool play game where the values are different. With Linq how can I group say the game by having the awayteampoolid with the group 122, and have hometeampoolid grouped with 144? It's easy if they are the same but different Im not sure.

122 Game 1 (122/122) Game 2 (122/144) 144 Game 2 (122/144) Game 3 (144/144) 
4
  • Can you show some sample data along with a sample result? Commented Oct 27, 2014 at 13:25
  • Is the information at the end not helpful? Commented Oct 27, 2014 at 13:38
  • Are you saying you want a single item to [possibly] appear in 2 places, ie once for [and 'under'] each distinct PoolId? Commented Oct 27, 2014 at 13:47
  • Yes basically, only way to do it Commented Oct 27, 2014 at 13:55

2 Answers 2

3

You need to Concat (union all) the list of AwayTeamPool and HomeTeamPool and then group on that like this:

var query = games.Select(x => new { Game=x, PoolId = x.AwayTeamPoolId }) .Concat(games.Where(x => x.HomeTeamPoolId != x.AwayTeamPoolId) .Select(x=> new { Game=x, PoolId = x.HomeTeamPoolId }) .GroupBy(x => x.PoolId, x => x.Game); 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Enumerable.GroupBy combined with anonymous types.

var groupedGames = games.GroupBy( game => new { game.AwayTeamPoolId, game.HomeTeamPoolId }); 

To get the games where AwayTeamPoolId is 122 and HomeTeamPoolId is 144:

var games = groupedGames.Single( group => group.Key.AwayTeamPoolId = 122 && group.Key.HomeTeamPoolId = 144) 

4 Comments

But wouldnt that create a group that contains 122 and 144? I want all games with 122 together (even if the home team has 144) and vice versa.
@MikeFlynn It'll create a group for each possible combination of AwayTeamPoolId+HomeTeamPoolId. If for some reason this is not what you want, please provide sample input and expected output so we can help you out.
Didnt I do that above?
@MikeFlynn I think I understand now, and I think Aducci nailed it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.