简体   繁体   中英

How do I provide a list as my parameter in sqlite-net

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.

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})";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM