user2285357
user2285357

Reputation: 45

Can we write Update statement in linq?

can we write update statement in linq?

example:

var query = Update customer set isEdit = 1 where id = 1

Thanks

Upvotes: 2

Views: 6837

Answers (3)

Cyril Gandon
Cyril Gandon

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

laktak
laktak

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

Daniel Hilgarth
Daniel Hilgarth

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

Related Questions