Copy Rows in a One to Many with LINQ to SQL
Posted
by Refracted Paladin
on Stack Overflow
See other posts from Stack Overflow
or by Refracted Paladin
Published on 2010-05-27T13:09:36Z
Indexed on
2010/05/27
13:21 UTC
Read the original article
Hit count: 206
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();
}
}
© Stack Overflow or respective owner