public async Task <IActionResult> CreateTeam(int Id, int[] teamCnt, string[] selectLst) { int teamcnt = 0; int cnt = 0; int round = 1; SubGame subgame = new SubGame(); List <Team> team = new List <Team>(); List <TeamMember> teamMembers = new List <TeamMember>(); if (_context.SubGames.Where(s => s.GameID == Id).Count() > 0) { round = _context.SubGames.Where(s => s.GameID == Id).Max(s => s.Round) + 1; } subgame.GameID = Id; subgame.Round = round; await _context.SubGames.AddAsync(subgame); int halfcnt = teamCnt.Count() % 2 == 0 ? teamCnt.Count() / 2 : teamCnt.Count() / 2 + 1; foreach (var item in teamCnt) { int idx = 0; if (teamcnt < halfcnt) { idx = teamcnt * 2; } else { idx = (teamcnt - halfcnt) * 2 + 1; } team.Add(new Team { SubGameID = subgame.ID, TeamName = ((Char)(65 + idx)).ToString(), TeamOrder = idx }); await _context.AddAsync(team[teamcnt]); for (int i = 0; i < item; i++) { teamMembers.Add(new TeamMember { BowlerID = selectLst[cnt], Score = 0, Average = _context.BowlerAverages.Find(selectLst[cnt]).Average, TeamID = team[teamcnt].ID, Sequence = i }); cnt++; } teamcnt++; } await _context.AddRangeAsync(team); await _context.AddRangeAsync(teamMembers); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index), new { Id = Id })); }