2

I am building a Xamarin Forms application and using Sqlite-net. I want to run a delete query that deletes all records that have a field in a list so something like below:

//usersToDelete is a list of Objects each representing a user.
List<int> idsToDelete = new List<int>();
foreach (var user in usersToDelete)
{
    idsToDelete.Add(user.Id);
}
string dbQuery = "DELETE FROM Users WHERE Id IN (?)";
var deleteCount = await LocalDatabaseConnection.ExecuteAsync(dbQuery, idsToDelete);

This does not work for me. It fails with the error Cannot store type: System.Collections.Generic.List1[System.Int32]. Does this mean I have build the string in code in palce of "(?)"? Or is there some way to provide this as a parameter.

Kikanye
  • 1,198
  • 1
  • 14
  • 33
  • 1
    I think you need to build the string yourself. See https://stackoverflow.com/questions/4788724/sqlite-bind-list-of-values-to-where-col-in-prm – Jason Dec 23 '21 at 21:53

1 Answers1

1

try this

var utd= usersToDelete.Select(i=> i.Id.ToString()).ToArray();
string ids=string.Join(",",utd);
string dbQuery = $"DELETE FROM Users WHERE Id IN ({ids})";
Serge
  • 40,935
  • 4
  • 18
  • 45