How would I write the following query using Linq to SQL?
UPDATE [TB_EXAMPLE] SET [COLUMN1] = 1
(My actual goal is more complex than this)
How would I write the following query using Linq to SQL?
UPDATE [TB_EXAMPLE] SET [COLUMN1] = 1
(My actual goal is more complex than this)
Yes. The DataContext has an ExecuteCommand method that will allow you to execute arbitrary (hopefully, parameterized) SQL.
Quoting from the remarks on the DataContext link above:
edt, oops to slow ;)
Assuming a DataContext connection, you can use the ExecuteCommand method of a DataContext object to execute SQL commands that do not return objects.
http://msdn.microsoft.com/en-us/library/bb386906.aspx
The DataContext class has two query methods, ExecuteCommand, and ExecuteQuery.
The ExecuteQuery method returns LINQ to SQL Entities, so you need to pass a type to it:
However, the ExecuteCommand doesn't need a type, you'd use this method for your UPDATE query Because you only need a String to write the query, you can use reflection to make a really generic UPDATE method for your DAL.
or
You can update the object directly then call save changes.
If you're looking to do this is and actual SQL command then Scott Guthrie has a great post that demonstrates using the ExecuteQuery and ExecuteCommand methods at http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx