LINQ列出逻辑

时间:2017-03-26 15:58:21

标签: c# linq list console

我是LINQ的新手。我只是想问一个关于LINQ用法的问题。

情况是我有2个玩家。给定的是Player1和Player2。它们都包含TeamName(字符串)和得分(int)。

我为玩家1制作了一个列表,

public static List<Player1> play1 = new List<Player1>
    {
        new Player1 { TeamName = "Manchester United", Score = 1 },
        new Player1 { TeamName = "Arsenal", Score = 1 },
        new Player1 { TeamName = "Manchester United", Score = 3 },
        new Player1 { TeamName = "Liverpool", Score = 2 },
        new Player1 { TeamName = "Swansea", Score = 2 }
    };

Player2的另一个列表

public static List<Player2> play2 = new List<Player2>
    {
        new Player2 { TeamName = "Chelsea", Score = 0 },
        new Player2 { TeamName = "Manchester United", Score = 1 },
        new Player2 { TeamName = "Fulham", Score = 1 },
        new Player2 { TeamName = "Manchester United", Score = 1 },
        new Player2 { TeamName = "Manchester United", Score = 4 }
    };

所以我接下来要做的就是创建一个游戏列表:

public static List<Game> game = new List<Game>
    {

    };

但我对下一步该做的事情一无所知。

我的LINQ代码是

IEnumerable<Game> x = from games in game
                               select games;

我想要达到的目标是我想得到多少次曼联赢了,失败,平局以及它的总得分。但是我对下一步要做的事情一无所知。我甚至不知道我做得对。

如果我正在做的事情是正确的以及我将如何解决这个问题,我正在寻找任何建议。谢谢。

1 个答案:

答案 0 :(得分:0)

您没有定义游戏数据结构。

从我所看到的 - 有多种方法可以实现大多数事情......但我认为在这种情况下我们想要改变设计。

而不是玩家让我们拥有一个团队和一个游戏对象。 一个团队有一个名字(也许可能是游戏列表)。 一个游戏由两个团队和一个游戏结果组成。

让我们有类似

的东西
    private class Team
    {
        public string Name { get; set; }
    }

    private class Game
    {
        public Team Team1 { get; set; }
        public Team Team2 { get; set; }

        public GameResult Result { get; set; }
    }

    enum GameResult
    {
        Team1Won,
        Draw,
        Team2Won
    }

此外,我们可以从团队角度定义结果

    enum TeamResultInAGame
    {

        TeamLost = 0,
        Draw = 1,
        TeamWon = 3,
    }

然后我们可以使用像

这样的linq查询来获取团队结果
        var teamResults = from game in games
                    where game.Team1.Name == relevantTeam || game.Team2.Name == relevantTeam
                    let teamWon = (game.Team1.Name == relevantTeam && game.Result == GameResult.Team1Won) ||
                                  (game.Team2.Name == relevantTeam && game.Result == GameResult.Team2Won)
                    let draw = game.Result == GameResult.Draw
                    select teamWon ? 
                           TeamResultInAGame.TeamWon :
                           draw ? TeamResultInAGame.Draw : TeamResultInAGame.TeamLost;

一个完整的例子:

        Team manchester = new Team() { Name = "Manchester" };
        Team chelsea = new Team() { Name = "Chelsea" };
        Team arsenal = new Team() { Name = "Arsenal" };

        List<Game> games = new List<Game>();
        games.Add(new Game() { Team1 = manchester, Team2 = chelsea, Result = GameResult.Draw });
        games.Add(new Game() { Team1 = arsenal, Team2 = manchester, Result = GameResult.Team1Won });
        games.Add(new Game() { Team1 = chelsea, Team2 = arsenal, Result = GameResult.Draw });
        games.Add(new Game() { Team1 = chelsea, Team2 = manchester, Result = GameResult.Draw });

        string relevantTeam = manchester;

        var teamResults = from game in games
                    where game.Team1.Name == relevantTeam.Name || game.Team2.Name == relevantTeam.Name
                    let teamWon = (game.Team1.Name == relevantTeam.Name && game.Result == GameResult.Team1Won) ||
                                  (game.Team2.Name == relevantTeam.Name && game.Result == GameResult.Team2Won)
                    let draw = game.Result == GameResult.Draw
                    select teamWon ? 
                           TeamResultInAGame.TeamWon :
                           draw ? TeamResultInAGame.Draw : TeamResultInAGame.TeamLost;

        Console.WriteLine("Wins Count: " + teamResults.Count(result => result == TeamResultInAGame.TeamWon));
        Console.WriteLine("Lost count: " + teamResults.Count(result => result == TeamResultInAGame.TeamLost));
        Console.WriteLine("Draw count: " + teamResults.Count(result => result == TeamResultInAGame.Draw));
        Console.WriteLine("Total score: " + teamResults.Sum(result => (int)result));