tags:

views:

40

answers:

4

I have two tables in my DB

Class
Student

One Class has many Students

What's the best,more elegant way to update the students of one class, using Linq?

A: 

Batch update isn't possible in Linq, so you have to select the students you want to update, iterate over them, update the fields, then submit changes on your data context.

(There have been some attempts to get batch updates working, for example this, but it's not officially supported by Microsoft.)

Mark Byers
+1  A: 

If you are using LINQ to SQL, then your Class Linq class should have a Students collection.

You should be able to update them all using a simple foreach loop. Once your updates are completed, just call SubmitChanges() on your Data Context.

Justin Niessner
+1  A: 
DataContext dc = new DataContext();

foreach(var student in dc.Students.Where(SomeCondition))
{
   student.SomeField = SomeValue;
   student.SomeOtherField = SomeOtherValue;
}
dc.SubmitChanges();
Robert Harvey
A: 

As they said batch updates aren't (easily) possible, so iterating over each student and updating one at a time is probably the best solution.

This article does come up with a way to do batch updates, but it looks to be more trouble than it's worth.

Adam Albrecht