Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Linq To SQL DRY
    text
    copied!<p>We decided to use Linq To SQL for our Data Layer on our most recent project. We have a functional solution, that so far has handled everything we have thrown at it, with one major problem. We have to code the same method over and over again to retrieve just slightly different result sets from our database. </p> <p>As an example:</p> <pre><code> public List&lt;TeamBE&gt; GetTeamsBySolutionID(Guid SolutionID) { List&lt;TeamBE&gt; teams = new List&lt;TeamBE&gt;(); 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; } </code></pre> <p>And on and on ad nauseum.</p> <p>Is there is a way to pass Linq results as a parameter to a function, so I can place my object mappings in one function, and not repeat myself so much?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload