C# GroupJoin efficiency

Posted by bsnote on Stack Overflow See other posts from Stack Overflow or by bsnote
Published on 2010-05-18T10:03:36Z Indexed on 2010/05/18 10:30 UTC
Read the original article Hit count: 157

Filed under:
|
|
|
|

without using GroupJoin:

var playersDictionary = players.ToDictionary(player => player.Id,
    element => new PlayerDto { Rounds = new List<RoundDto>() });

foreach (var round in rounds)
{
    PlayerDto playerDto;
    playersDictionary.TryGetValue(round.PlayerId, out playerDto);

    if (playerDto != null)
    {
        playerDto.Rounds.Add(new RoundDto { });
    }
}

var playerDtoItems = playersDictionary.Values;

using GroupJoin:

var playerDtoItems =
    from player in players
    join round in rounds
    on player.Id equals round.PlayerId
    into playerRounds
    select new PlayerDto { Rounds = playerRounds.Select(playerRound => new RoundDto {}) };

Which of these two pieces is more efficient?

© Stack Overflow or respective owner

Related posts about c#

Related posts about group