2016-02-08 39 views
0

我很努力地嘗試,但無法達到我的結果。我是linq新手。應該是什麼這個SQL查詢正確的LINQ查詢。請幫我解決這個問題。LINQ到SQL,不能解決大LINQ的sql語句

update Teacher set RemainingCredit = RemainingCredit- Course.Credit 
from 
CourseAssignTeacher 
join Course on CourseAssignTeacher.CourseId = Course.Id 
join Teacher on CourseAssignTeacher.TeacherId = Teacher.Id 
where CourseAssignTeacher.Id = 1 

回答

0

編寫選擇要更新什麼的查詢,然後使用foreach環和context.SubmitChanges

using (DBDataContext db = new DBDataContext()) 
{ 
    var toUpdate = from cat in db.CourseAssignTeacher 
        join c in db.Course on cat.CourseId equals c.Id 
        join t in db.Teacher on cat.TeacherId equals t.Id 
        where cat.Id = 1 
        select new { Teacher = t, Course = c, CourseAssignTeacher = cat }; 
    foreach (var x in toUpdate) 
    { 
     x.Teacher.RemainingCredit = x.Teacher.RemainingCredit - x.Course.Credit; 
    } 

    db.SubmitChanges(); 
}