Skip to content
Advertisement

SQL Select where values in List

Is there a way that I can create query against a data source (could be sql, oracle or access) that has a where clause that points to an ArrayList or List?

example:

Select * from Table where RecordID in (RecordIDList)

I’ve seen some ways to do it with Linq, but I’d rather not resort to it if it’s avoidable.

Advertisement

Answer

You could use String.Join. Try something like this:

String query = "select * from table where RecordId in ({0});";
String formatted = String.Format(query, String.Join(",", list.ToArray()));

As a side note this will not protect you against SQL injection – hopefully this example will point you in the right direction.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement