不使用羣組加入:C#效率羣組加入
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;
使用羣組加入:
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 {}) };
這兩個部件的效率更高?
你的意思是高效?如果它有效,它的有效性。 – 2010-05-18 10:04:42
是的,高效。對不起 – 2010-05-18 10:05:55