3

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);
0

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. The status id value is in a look up table with name StatusType with columns statusid and statusdesc. How can i link this in the query directly. For instance where user.statusid=statustype.statusid.
0

Would this work?

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

1 Comment

Note, will throw an exception if there's no matching record with StatusId==true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.