如何从较大的列表创建子列表以在MVC中循环

时间:2018-05-03 13:00:14

标签: c# asp.net model-view-controller

我一直试图用大脑包裹这一天大约一天,我似乎无法概念化我的问题的解决方案(也许我认为这一切都错了)。

我想做的是列出16名球员参加锦标赛并将其分成对决,比如较大的锦标赛名单的子列表。说我的列表看起来像这样(但16个玩家而不是4个):

        IList<TeamModel> tournamentList = new List<TeamModel>();            
        tournamentList.Add(new TeamModel() { ID = 1, Seed = 1, Conference = "Conference 1", TeamName = TruncateTeamName("Team 1") });
        tournamentList.Add(new TeamModel() { ID = 2, Seed = 2, Conference = "Conference 1", TeamName = TruncateTeamName("Team 2") });
        tournamentList.Add(new TeamModel() { ID = 3, Seed = 3, Conference = "Conference 1", TeamName = TruncateTeamName("Team 3") });
        tournamentList.Add(new TeamModel() { ID = 4, Seed = 4, Conference = "Conference 1", TeamName = TruncateTeamName("Team 4") });

我现在正在做的事情并没有真正发挥作用。我认为最好使用foreach循环遍历列表并将单个matchups(2个玩家)添加到新列表中。那时,我需要访问Razor中的TeamModel数据,但是现在我不能使用它。但我知道我现在正在考虑这个错误。

这是我到目前为止所做的:

        IList<TeamMatchup> TeamMatchup = new List<TeamMatchup>();
        List<TeamMatchup> matchup = new List<TeamMatchup>();
        foreach (TeamModel team in tournamentList)
        {                
            if (TeamMatchup.Count == 2)
            {
                matchup.AddRange(TeamMatchup);
                TeamMatchup.Clear();
                if (matchup.Count.Equals(tournamentList.Count / 2))
                    break;
            }
            else
            {

                for(int i = 0; i < 1; i++)
                {
                    if (i == 0)
                    {
                        //TeamMatchup.Add(new TeamMatchup() { TeamA = ??, TeamB = ?? });
                    }
                    else
                    {

                    }                        
                }                    
            }                    
        }
        ViewData["Matchup"] = matchup;
        //ViewData["Team"] = //need to add some method to handle the individual matchups

        return View();

任何帮助或建议将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我假设你正试图在淘汰赛中创造对决方式。所以第1队与第2队,第3队与第4队等等,而不是循环赛方式。

您可以使用for或foreach循环。在下面的代码中,我循环通过团队列表,如果计数奇怪,将该团队添加到当前比赛中的TeamA。如果计数甚至是将该球队添加到比赛的TeamB中。然后将比赛添加到比赛列表中。然后创建一个新的currentMatch。

        var matchups = new List<TeamMatch>();
        var matchCount = 1; // sets a count
        var currentMatch = new TeamMatch(); // current matchup

        foreach (var team in tournamentList)
        {                
            // Checks if count is an even number.
            if (matchCount % 2 == 0)
            {
                // If an even number, add teamB to matchup, 
                // add current matchup to list and create a new matchup.
                currentMatch.TeamB = team;
                matchups.Add(currentMatch);

                currentMatch = new TeamMatch();
            }
            else
            {
                // If on odd number, add teamA to matchup.
                currentMatch.TeamA = team;
            }

            matchCount++; 
        }

        if (matchCount % 2 == 1)
        {
            //Odd number of total teams, one team isn't matched.
        } 

        // Here is where you can set the viewdata of the matchups.