2017-03-26 88 views
-1

我是LINQ的新手。我只想問一個關於LINQ使用的問題。LINQ列表邏輯

情況是我有2個玩家。給定是玩家1和玩家2。它們都由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; 

我想實現的是我希望得到曼聯多少次贏了,輸了,畫,它的總得分。但我對接下來要做的事情無能爲力。我甚至不知道我是否做得對。

我在尋找任何建議,如果我正在做的是正確的,我將如何解決這個問題。謝謝。

+2

爲什麼要創建兩類玩家'玩家1'和'玩家2'?它看起來很糟糕的設計。我建議你創建一個有Man的類'Player'。美國隊,阿森納隊等等,然後你創建一個「比賽」對象,並且與比分一起提到兩名球員。 –

+1

你的'Player'類代表什麼?爲什麼它有一個分數?你想達到什麼目標,你想要建模什麼?我們從收集和分析需求開始。 – ironstone13

+0

你如何計算一支球隊獲勝或失敗的次數? – Flaugzig

回答

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));