Updating a single column in LINQ to SQL

I have a table with several columns, and I want to update only one column. I am writing a method that first retrieves a record and then updates it by changing the value of the column. Like this:

using (myDC...)
{
  var recordInDB = (from...
                    where ....
                    select r).SingleOrDefault();

  if (recordInDB != null)
  {
     recordInDB.MyColumn = newValue;

     myDC.SubmitChanges();
  }

Does it remain to save all the other columns and update only the ones I want to change, or will this clear all the columns and update the MyColumn column with a new value?

+5
source share
2 answers

This will not change the rest of the columns in your table. Only the one you are updating.

+6
source

This will save the columns that you did not touch on how they are in the database while reading.

In SubmitChangesa LINQ2SQL call, it compares the values ​​in the fields of the object with the values ​​in the database and updates only those that have changed.

+4
source

All Articles