public EditTeam CreateEditTeam(int?id) { Team team; using (var context = new TeamContext()) { var service = new TeamService(context); team = service.GetAll().SingleOrDefault(t => t.Id == id); } EditTeam editTeamViewModel = new EditTeam(); editTeamViewModel.team = team; RaceEntry raceEntry; using (var context = new RaceEntryContext()) { var service = new RaceEntryService(context); raceEntry = service.GetByTeamId(team.Id); } using (var context = new RaceEventContext()) { editTeamViewModel.raceEvent = context.RaceEvents.SingleOrDefault(r => r.Id == raceEntry.RaceId); } editTeamViewModel.registeredUsers = new List <RegisteredUser>(); editTeamViewModel.users = new List <User>(); IEnumerable <int> registeredUserIds; using (var context = new TeamCrewRegisteredUserContext()) { var service = new TeamCrewRegisteredUserService(context); registeredUserIds = service.GetAllregisteredUserIdsByTeamId(team.Id); } using (var context = new RegisteredUserContext()) { var RUservice = new RegisteredUserService(context); var userService = new UserService(new UserContext()); foreach (int rUserId in registeredUserIds) { int userId = RUservice.GetUserIdById(rUserId); RegisteredUser registeredUser = RUservice.GetById(rUserId); User user = userService.GetAll().SingleOrDefault(u => u.Id == userId); editTeamViewModel.registeredUsers.Add(registeredUser); editTeamViewModel.users.Add(user); } } return(editTeamViewModel); }