user1663996
user1663996

Reputation: 43

Query update multiple rows

I have table with token, userName, StatusId. Before i insert any row in table for a specific user i would like to check if any rows with active status are there if so update it to inactive.

I am unable to figure out how can i do this in LINQ. Currently iam retreving my values as the query below.

var activeUsers = 
   (from _users in currentDataContext.Users 
        where _users.StatusId.Equals(1) 
        select _session);

Upvotes: 3

Views: 108

Answers (2)

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

users = currentDataContext.Users.Where(o => o.StatusId == 1);
foreach(var item in users)
  item.StatusId = 0;
currentDataContext.SaveChanges();

Beware that If you app is multi user then there is a chance that DB has been changed after you queried the database.

Upvotes: 2

Jerry
Jerry

Reputation: 6577

Would this work?

currentDataContext.Users.Where(x=> x.StatusId == true).First().StatusId = False;
currentDataContext.SaveChanges();

Upvotes: 0

Related Questions