EF - Update multiple rows in database without using foreach loop

c# entity-framework linq

Question

My test program:

using (var db = new MyDbContext())
{
  string fromUser = ""; //sender
  string toUser = ""; //receiver

  var messages = db.Message.Where(x => x.FromUser == fromUser && x.ToUser == toUser)
                 .ToList();

  foreach (var message in messages)
  {
    message.IsRead = true;
    db.SaveChanges();
  }
}

I'm curious whether there's another approach to updating databases that doesn't useforeach loop in that situation?

P.S. I had used these questions as references, but none of them helped me:

1
11
5/23/2017 11:54:50 AM

Accepted Answer

ZZZ_tmp
24
12/5/2015 4:43:01 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow