shariaretanvir akash February 2016

linq to sql , can not solve big sql statement into linq

I am trying very hard but cannot reach my result. I am new to linq. What should be the proper linq query for this sql query. Please help me out of this.

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

Answers


Tim Schmelter February 2016

Write a query that selects what you want to update, then use a foreach loop and 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();
}

Post Status

Asked in February 2016
Viewed 1,553 times
Voted 14
Answered 1 times

Search




Leave an answer