I have the following two methods that get data from my database and return a filled SelectList object (including the value of the "All" option), which I then pass to my view. The problem is that they are almost identical with the exception that they both refer to different repository objects and have different identification names (StatusId and TeamId). I think it is possible to reorganize them into one method, which takes the repository as a parameter and somehow determines what the identification name is, perhaps using reflection or some kind of lambda expression, but I do not know how to do this.
private SelectList GetStatusSelectList(int selectedStatusId)
{
List<MemberStatus> statusList = _memberStatusRepository.All().ToList();
statusList.Insert(0, new MemberStatus {StatusId = 0, Name = "All"});
var statusSelectList = new SelectList(statusList, "StatusId", "Name", selectedStatusId);
return statusSelectList;
}
private SelectList GetTeamSelectList(int selectedTeamId)
{
List<MemberTeam> teamList = _memberTeamRepository.All().ToList();
teamList.Insert(0, new MemberTeam { TeamId = 0, Name = "All" });
var teamSelectList = new SelectList(teamList, "TeamId", "Name", selectedTeamId);
return teamSelectList;
}
Can someone help figure out how to reorganize them into one method?