0

I have a list of objects, for example

list<Employee> Employees = 
[
 {
   id:1,
   name: Luke,
   age: 5
 },
 {
   id:2,
   name: Elizabeth,
   age: 15
 },
]

How can I perfom a bulk update, should I use a foreach, or is there a way to perfom the update in a single line, the objets will have more properties and every property may change

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • By LINQ C#, do you mean Entity Framework? Out of the box, EF doesn't do that. However, there are plug-ins to do it. Example: https://entityframework.net/bulk-update – Flydog57 Aug 17 '21 at 03:29
  • @Flydog57 that's right I mean the EF – darkknightlullaby Aug 17 '21 at 03:33
  • Hi @darkknightlullaby, that question was answered a few years ago :) https://stackoverflow.com/questions/7193369/bulk-update-with-linq-to-sql – Eking Aug 17 '21 at 04:44
  • Read all the answers in the linked duplicate - you probably want the one recommending the BulkExtensions library – Caius Jard Aug 17 '21 at 05:05

1 Answers1

0

yes - foreach and single line:

Employees.ForEach(emp => emp.age++);
Felix
  • 9,248
  • 10
  • 57
  • 89
  • ok the list actually have the data that will update the database, the foreach is the correct way to update the records? – darkknightlullaby Aug 17 '21 at 03:32
  • This will change the in-memory representation of your data. It doesn't handle the part of your question that became clear through the comments that you wish to update a database table. – Eric J. Aug 17 '21 at 03:43