我有一个GameResult类型的List <>,我要使用来自数组(gamerArray)的数据填充GameResult类的新实例。
所以我有一个foreach循环。
List<GameResult> gameResults = new List<GameResult>();
foreach (var g in gamerArray.GamePlayers)
{
gameResults.Add(
new GameResult
{
GameID = g.GameID,
GameName = g.GameName,
SessionStart = g.StartTime,
SessionEnd = g.EndTime,
SessionMessage = "You Won!"
});
}
是否可以使用Linq遍历数组并创建新的GameResult对象列表?
谢谢
答案 0 :(得分:-1)
尝试一下:
List<GameResult> gameResults = gamerArray.GamePlayers.Select(g => new GameResult
{
GameID = g.GameID,
GameName = g.GameName,
SessionStart = g.StartTime,
SessionEnd = g.EndTime,
SessionMessage = "You Won!"
}).ToList();
答案 1 :(得分:-1)
gameResults = gamerArray.GamePlayers.Select(g => new GameResult
{
GameID = g.GameID,
GameName = g.GameName,
SessionStart = g.StartTime,
SessionEnd = g.EndTime,
SessionMessage = "You Won!"
}).ToList();