Question Is it possible to return all items in a List/Queue without Iterating over them?

CodeLiftsleep

Member
Joined
Mar 11, 2016
Messages
20
Programming Experience
3-5
Ok, I have a very specific situation that this would be useful for.

I have a DataTable with a large number of columns(200+) that I am doing a bulk insert for in SQLite, since it does not support Update Table like SQL Server does.

I want to know if there is a way after I iterate over all the columns in a row and stick their values into a list(list of 200+ items---some integers, some floats, some strings) if there is a way to take those list values and put them all into an SQL statement such as:

"INSERT INTO TableName VALUES(List values each separated by a comma)"

I know I can iterate over the list again and then stick each item into a string or object but that is extra processing to be done. In python you can just get the values from the list by returning the list, but that doesn't work in VB. I was wondering(I am thinking there would have to be) if there is a way to do this without having to create a separate object to then hold all the values from the list after iterating over them and getting them out.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
It's not possible to get the values from the list in any language without iterating over the list in some way. Even if you don't write a loop, that's still what's going to happen under the hood. In your case, there's a very easy way to simplify the code. Assuming that 'myList' is an IEnumerable(Of String), this code will do what you want:
Dim sql = String.Format("INSERT INTO TableName({0})", String.Join(", ", myList))
As I said, there's no loop in your code but the list is still going to be enumerated within the String.Join method.
 

CodeLiftsleep

Member
Joined
Mar 11, 2016
Messages
20
Programming Experience
3-5
It's not possible to get the values from the list in any language without iterating over the list in some way. Even if you don't write a loop, that's still what's going to happen under the hood. In your case, there's a very easy way to simplify the code. Assuming that 'myList' is an IEnumerable(Of String), this code will do what you want:
Dim sql = String.Format("INSERT INTO TableName({0})", String.Join(", ", myList))
As I said, there's no loop in your code but the list is still going to be enumerated within the String.Join method.


Bad choice of words, but you got what I meant. Actually found this right before you posted by I like the ({0}) you had better...I always forget about that and just concatenate strings with the & symbols...

Had to stick VALUES directly after Tablename or else it thought they were columns, but it worked great other than that. Thanks!
 
Top Bottom