How can I avoid setting some columns if others haven't changed, when working with Linq To SQL?

Posted by Patrick Szalapski on Stack Overflow See other posts from Stack Overflow or by Patrick Szalapski
Published on 2010-05-20T18:10:31Z Indexed on 2010/05/20 18:30 UTC
Read the original article Hit count: 171

In LINQ to SQL, I want to avoid setting some columns if others haven't changed? Say I have

dim row = (From c in dataContext.Customers Where c.Id = 1234 Select c).Single()
row.Name = "Example"
' line 3
dataContext.SubmitChanges()   ' line 4

Great, so LINQ to SQL fetches a row, sets the name to "Example" in memory, and generates an update SQL query only when necessary--that is, no SQL will be generated if the customer's name was already "Example".

So suppose on line 3, I want to detect if row has changed, and if so, set row.UpdateDate = DateTime.Now. If row has not changed, I don't want to set row.UpdateDate so that no SQL is generated. Is there any good way to do this?

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about .NET