【发布时间】:2010-10-27 02:34:04
【问题描述】:
我们决定在我们最近的项目中使用 Linq To SQL 作为我们的数据层。我们有一个功能性的解决方案,到目前为止,它已经处理了我们提出的所有问题,但有一个主要问题。我们必须一遍又一遍地编写相同的方法来从我们的数据库中检索略有不同的结果集。
举个例子:
public List<TeamBE> GetTeamsBySolutionID(Guid SolutionID)
{
List<TeamBE> teams = new List<TeamBE>();
Esadmin db = new Esadmin(_connectionString);
var qry = (from teamsTable in db.Teams
join solutionsTable in db.Solutions on teamsTable.SolutionID equals solutionsTable.SolutionID
where teamsTable.SolutionID == SolutionID
select new { teamsTable, solutionsTable.SolutionName });
foreach (var result in qry)
{
TeamBE team = new TeamBE();
team.TeamID = result.teamsTable.TeamID;
team.Description = result.teamsTable.Description;
team.Status = result.teamsTable.Status;
team.LastModified = result.teamsTable.LastModified;
team.SolutionID = result.teamsTable.SolutionID;
team.SolutionName = result.SolutionName;
team.Name = result.teamsTable.Name;
team.LocationLevel = result.teamsTable.LocationLevel;
team.AORDriven = result.teamsTable.AoRDriven;
team.CriteriaID = result.teamsTable.CriteriaID ?? Guid.Empty;
teams.Add(team);
}
return teams;
}
public TeamBE GetTeamByID(Guid TeamID)
{
Esadmin db = new Esadmin(_connectionString);
TeamBE team = new TeamBE();
var qry = (from teamsTable in db.Teams
join solutionsTable in db.Solutions on teamsTable.SolutionID equals solutionsTable.SolutionID
where teamsTable.TeamID == TeamID
select new { teamsTable, solutionsTable.SolutionName }).Single();
team.TeamID = qry.teamsTable.TeamID;
team.Description = qry.teamsTable.Description;
team.Status = qry.teamsTable.Status;
team.LastModified = qry.teamsTable.LastModified;
team.SolutionID = qry.teamsTable.SolutionID;
team.SolutionName = qry.SolutionName;
team.Name = qry.teamsTable.Name;
team.LocationLevel = qry.teamsTable.LocationLevel;
team.AORDriven = qry.teamsTable.AoRDriven;
team.CriteriaID = qry.teamsTable.CriteriaID ?? Guid.Empty;
return team;
}
不断地令人作呕。
有没有办法将 Linq 结果作为参数传递给函数,这样我就可以将我的对象映射放在一个函数中,而不会重复太多?
【问题讨论】:
标签: c# .net linq linq-to-sql .net-3.5