0

C#

Ok, I have something like that:

var message = db.Table<Message>().Where(cond => cond.ID == messageID).Single();
message.Status = status;

But I'd like to change my messageID from int to int[], so I will select all data where cond.ID is in my int[] messageID. I need something like .Where(cond => cond.ID IN messageID). I don't want to do that in a way like:

foreach(var ID in messageID)
{
   message = db.Table<Message>.Where(cond => cond.ID == ID).Single();
   message.Status = status;
   db.Update(message);
}

I use SQLite, any help with update multiple rows at once will be very appreciated.

4 Answers 4

3

This will do it:

var messages = db.Table<Message>().Where(msg => messageIDs.Contains(msg.ID));
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, I'm updating ObservableCollection. Is there a way to update all items with single line of code? ;)
@VladislavKasianenko You could potentially do (at the end) .ToList().ForEach(msg => msg.Status = status); - but I personally find that a bit ugly (creating a list just to update a property) and usually just use a foreach.
2

I think this should work.

var message = db.Table<Message>().Where(cond => messageIDs.Any(id => cond.ID == id).Single();
message.Status = status;

Comments

2

Something like this:

var messageIDs = new int[]; // assume it's filled
var result = db.Table<Message>().Where(cond => messageIDs.Contains(cond.ID));

foreach(var message in result)
    message.Status = status;

db.SaveChanges(); //or something similar.

It will return all the messages which ID's are present in the test array.

Be advised: Contains can translate to OR in SQL. It might perform badly. Especially when applied to string.

Meanwhile you can set the status for each message using a foreach loop. The ORM you are using might have something like db.SaveChanges which should send a single update query to the database.

Comments

0

use the .contains() method

var message = db.Table<Message>().Where(cond => messageID.contains(cond.ID)).Single();

Comments

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.