Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a List(of Long) into a string array? (VB)

Tags:

vb.net

I want to convert a List (of Long) into a string array.

Reason: it's a list of database IDs and I want to make a comma delimited string to pass into a stored proc.

I tried this:

Dim commaDelimitedList As String = String.Join(",", itemIDList.Cast(Of String)().ToArray)

but I'm clearly not using the Cast correctly since it throws an exception: System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'..

Is there a way to get Cast working for this, or am I stuck with ConvertAll and a delegate function?

like image 603
elwy Avatar asked Oct 26 '09 17:10

elwy


2 Answers

If you can use LINQ this will do what you want:

Dim commaDelimitedList  As String = String.Join(",", itemIDList.Select(Function(itemID) itemID.ToString()).ToArray())
like image 85
Fredrik Mörk Avatar answered Oct 12 '22 22:10

Fredrik Mörk


Can't cast - unless you can LINQ, you have to convert each int into a string so you have an array of strings CLR 2.0 has a ConvertAll() method that will do that...

string s =  String.Join(",",
              l1.ConvertAll<string>(delegate(int i) 
                 { return i.ToString(); }).ToArray()); 
like image 42
Charles Bretana Avatar answered Oct 12 '22 23:10

Charles Bretana