My test code is:
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(); } }
My question is: is there another way to update the database without using a foreach in this case?
foreach
p / s: I mentioned these questions, but they all did not solve my problem:
Updating multiple rows in datatable without loop
updating multiple database records with linq to sql without a loop, is this possible?
Update multiple rows without loop
You do not need SaveChanges () at each iteration. This will hit the database several times. Try:
using (var db = new MyDbContext()) { string fromUser = ""; //sender string toUser = ""; //receiver var messages = db.Message.Where(x => x.FromUser == fromUser && x.ToUser == toUser) .ToList(); messages.ForEach(m => m.IsRead = true); db.SaveChanges(); }
Source: https://habr.com/ru/post/1237465/More articles:Mirroring rows in a matrix using loops / recursion? - pythonLaravel database with json type field adds \ "when returning value - jsonRColorBrewer extension to support more colors? - colorsAllocating memory to string objects? - javaPlay Framework 2.4: NoClassDefFoundError: play / api / cache / CachePlugin - javaWhy can you assign an array to a char pointer? - c ++Updating multiple rows in datatable without loop - c #Converting char [,] array to char ** - arraysSpeeding Levy's motion algorithm simulation - performanceHow to create a regular expression without a specific group of letters in lex - regexAll Articles