code_junkie

How to update a single column in LINQ without loading the entire row?

Nov 11th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. using (MyDataContext wdc = new MyDataContext())
  2. {
  3. Article article = wdc.Article.First(p => p.ID == id);
  4. article.ItemsInStock = itemsinstock;
  5. wdc.SubmitChanges();
  6. }
  7.  
  8. wdc.ExecuteCommand("UPDATE Article SET ItemsInStock = @1 WHERE ID = @2", itemsinstock,id);
  9.  
  10. UPDATE ... SET ... WHERE ID = @2
  11.  
  12. UPDATE ... SET ... WHERE ID = @2 AND ItemsInStock = @1 AND SomeOtherColumn = @3 AND...
  13.  
  14. context.Articles.Attach(article /* article with updated values */, new Article { ID = articleID, ItemsInStock = -1 } /* pretend that this is the original article */);
  15. context.SubmitChanges();
Add Comment
Please, Sign In to add comment