Copy Rows in a One to Many with LINQ (2 SQL)
- by Refracted Paladin
I have a table that stores a bunch of diagnosis for a single plan. When the users create a new plan I need to copy over all existing diagnosis's as well. I had thought to try the below but this is obviously not correct. I am guessing that I will need to loop through my oldDiagnosis part, but how?
Thanks!
My Attempt so far...
public static void CopyPlanDiagnosis(int newPlanID, int oldPlanID)
{
using (var context = McpDataContext.Create())
{
var oldDiagnosis = from planDiagnosi in context.tblPlanDiagnosis
where planDiagnosi.PlanID == oldPlanID
select planDiagnosi;
var newDiagnosis = new tblPlanDiagnosi
{
PlanID = newPlanID,
DiagnosisCueID = oldDiagnosis.DiagnosisCueID,
DiagnosisOther = oldDiagnosis.DiagnosisOther,
AdditionalInfo = oldDiagnosis.AdditionalInfo,
rowguid = Guid.NewGuid()
};
context.tblPlanDiagnosis.InsertOnSubmit(newDiagnosis);
context.SubmitChanges();
}
}