G'day everyone.
I'm still learning LINQ so forgive me if this is naive. When you're dealing with SQL directly, you can generate update commands with conditionals, without running a select statement.
When I work with linq I seem to follow the pattern of:
- Select entities
- Modify entities
- Submit changes
What I want to do is a direct update using linq and deferred execution. Is it possible that the actual execution occurs directly at the SQL without any data being transmitted up to the client?
DataContext dc = new DataContext
var q = from product in dc.Products
where product.Type = 1
set product.Count = 0
dc.SubmitChanges
So in essence LINQ has all the information it needs WITHOUT using a select to generate an update command. It would run the SQL:
Update Products Set Count = 0 Where Type = 1
Does a keyword like "set" exist in LINQ?
Best Answer
You can actually let LINQ-to-SQL generate update statements:
In your Dbml set UpdateCheck="Never" for all properties.
This will generate a single update statement without having to do a select first.
One caveat: if you want to be able to set Name to null you would have to initialize your foo object to a different value so Linq can detect the change:
If you want to check for a timestamp while updating you can do this as well: