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

C# 如何从更大的列表中创建子列表以在MVC中循环,c#,asp.net,model-view-controller,C#,Asp.net,Model View Controller,一天来,我一直试图用大脑来思考这个问题,但我似乎无法为我的问题构思出一个解决方案(也许我在这个问题上完全错了) 我想做的是为一场比赛挑选一份16名球员的名单,并将他们分成几组,就像一份更大的比赛名单的子名单一样。假设我的名单是这样的(但不是4名球员,而是16名球员): IList tournamentList=新列表(); tournamentList.Add(new TeamModel(){ID=1,Seed=1,Conference=“Conference 1”,TeamName=Trunc

一天来,我一直试图用大脑来思考这个问题,但我似乎无法为我的问题构思出一个解决方案(也许我在这个问题上完全错了)

我想做的是为一场比赛挑选一份16名球员的名单,并将他们分成几组,就像一份更大的比赛名单的子名单一样。假设我的名单是这样的(但不是4名球员,而是16名球员):

IList tournamentList=新列表();
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循环遍历列表,并将单个匹配(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();
IList TeamMatchup=new List();
列表匹配=新列表();
foreach(锦标赛列表中的团队模型团队)
{                
if(TeamMatchup.Count==2)
{
matchup.AddRange(TeamMatchup);
TeamMatchup.Clear();
if(matchup.Count.Equals(tournamentList.Count/2))
打破
}
其他的
{
对于(int i=0;i<1;i++)
{
如果(i==0)
{
//添加(新的TeamMatchup(){TeamA=??,TeamB=??});
}
其他的
{
}                        
}                    
}                    
}
ViewData[“匹配”]=匹配;
//ViewData[“Team”]=//需要添加一些方法来处理单独的匹配
返回视图();

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

我假设您正试图以消除括号的方式创建匹配。所以第一队对第二队,第三队对第四队,等等,不是循环赛

为此,可以使用for或foreach循环。在下面的代码中,我将循环遍历团队列表,如果计数是奇数,则将该团队添加到当前比赛中的TeamA中。如果伯爵甚至将该队加入比赛的B队。然后将匹配添加到匹配列表中。然后创建一个新的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.
var matchups=新列表();
var matchCount=1;//计数
var currentMatch=new TeamMatch();//当前匹配
foreach(锦标赛列表中的var团队)
{                
//检查计数是否为偶数。
如果(匹配计数%2==0)
{
//如果是偶数,则将teamB添加到匹配中,
//将当前匹配添加到列表并创建新匹配。
currentMatch.TeamB=团队;
添加(当前匹配);
currentMatch=新团队匹配();
}
其他的
{
//如果是奇数,则将teamA添加到匹配中。
currentMatch.TeamA=团队;
}
matchCount++;
}
如果(匹配计数%2==1)
{
//团队总数为奇数,一个团队不匹配。
} 
//在这里可以设置匹配的viewdata。

是的,这正是我想要的。谢谢
        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.