Flavio Justino Flavio Justino - 1 year ago 261
C# Question

C# SQLite: how to select values from table where values are in c# list

I am writing a C# form application using SQLite as my Database provider.
I have a Checkedlistbox listing all products and I read the checked Id's into a c# List. So far so good. Now I want to select from Table PRODUCTS all the products with the Ids from the list (so basically, I want to select the checked products). Previously I have done this using LINQ by using:

where lstids.Contains(p.id)

But in SQLite I am kinda lost. I have tried the following:

string query = "Select * from PRODUCTS where id in "+ lstids;

but it returns a null value.

Any ideas?

Best regards,

Flavio Justino

Answer Source

To do proper IN statement in SQLLite you need to give it as follows:

SELECT * FROM Products WHERE ID in (id1, id2, id3)

lstids is probably a list, collection and it's ToString() is not giving right data.

Try that:

var ids = string.Join(",", lstids.Select(x => x.ToString()).ToArray());
string query = string.Format("SELECT * FROM Products WHERE ID in ({0})", ids)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download