2

can we write update statement in linq?

example:

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

Thanks

bash.d
  • 13,029
  • 3
  • 29
  • 42
user2285357
  • 45
  • 1
  • 4

3 Answers3

5

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;
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
3

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);
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
1

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.

Community
  • 1
  • 1
laktak
  • 57,064
  • 17
  • 134
  • 164