Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an array of integers for use in a SQL "IN" clause

Tags:

arrays

c#

sql

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause?

e.g.

int[] values = {1,2,3};

would go to

"(1,2,3)"
like image 717
AJM Avatar asked Nov 13 '09 15:11

AJM


1 Answers

var inClause = "("
     + String.Join(",", values.Select(x => x.ToString()).ToArray()) 
     + ")";

Note: You no longer need to call .ToArray() in .NET Framework 4. A new String.Join<T>(string separator, IEnumerable<string> values) method is added.

like image 83
mmx Avatar answered Oct 20 '22 03:10

mmx