Reputation: 45
can we write update statement in linq?
example:
var query = Update customer set isEdit = 1 where id = 1
Thanks
Upvotes: 2
Views: 6837
Reputation: 17058
You can shortcut the updates by doing a 'looks like Linq' query, using the ForEach method of the class List:
var toUpdate = customer.Where(c => c.id == 1).ToList();
toUpdate.ForEach(c => c.isEdit = 1);
Upvotes: 3
Reputation: 60043
Yes you can, see my answer here.
Foo foo=new Foo { FooId=fooId }; // create obj and set keys
context.Foos.Attach(foo);
foo.Name="test";
context.SubmitChanges();
In your Dbml set UpdateCheck="Never" for all properties.
This will generate a single update statement without having to do a select first.
Upvotes: 1
Reputation: 174427
No, you can't. The Q in LINQ stands for Query.
What you can do is the following:
foreach(var c in customer.Where(x => x.Id == 1))
c.isEdit = 1;
Upvotes: 5