How to update a number of Rows in a Table using Linq
I am using the following code to update the UserSession column of the Activities. Following code return the records if the ExpiryTimeStamp is less then current date. Then it Update the UserSession column to 0 for the returned recods in the table. .Now I wants that if there 开发者_如何学Goare 100 records are returned then these should update at one time instead of using the FoREach. Is it posible in Linq
CacheDataDataContext db = new CacheDataDataContext();
var data = (from p in db.Activities
where p.ExpiryTimeStamp < DateTime.Now
select p).ToList();
data.ForEach(ta => ta.UserSession = "0");
db.SubmitChanges();
In short, no: Linq-2-sql does not do batch updates out of the box.
(I am not sure your foreach will work like you wrote - i do not think so - but this is similar and will work)
foreach (var x in data)
{x.UserSession = "0";}
db.SubmitChanges()
BUT, even if you do it like this, Linq-2-sql will send an update statement for each record to the database. So with your example of 100 records returned you will get 100 individual updates send to the database.
精彩评论