Click here to Skip to main content
14,330,512 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have List3 as List(Of List(Of Integer))
Dim List3 As New List(Of List(Of Integer))
Dim List1 As List(Of Integer) = {1, 2, 5, 7}.ToList
Dim List2 As List(Of Integer) = {5, 3, 2, 1}.ToList
List3.Add(List1)
List3.Add(List2)


How can i get string like this
1,2,5,7 
5,3,2,1


What I have tried:

Dim str As String = String.Join(vbNewLine, List3)
Posted
Updated 8-Oct-19 8:24am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

You have to iterate the collection and format the output:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Public Shared Function DumpListOfList(Of T)(List(Of List(Of T)) list) As String
   Dim builder As New StringBuilder()
   For Each sublist As List(Of T) In list
      builder.AppendLine(String.Join(",", sublist.ToArray()))
   Next
   builder.Remove(builder.Length - 2, 2) '' Get rid of the last CrLf
   Return builder.ToString()
End

'' Usage:
Dim result As String = DumpListOfList(List3)
   
v3
Comments
Maciej Los 8-Oct-19 16:06pm
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 3

A simpler solution using LINQ:
Dim str As String = String.Join(vbNewLine, List3.Select(Function (list) String.Join(",", list)))
Lambda Expressions (Visual Basic) | Microsoft Docs[^]
   
Comments
Maciej Los 8-Oct-19 16:06pm
   
5ed!
phil.o 8-Oct-19 17:42pm
   
5'd also.
There's so much time I left VB now, generics did not exist yet; when I have to write some code block in VB, I think about it in c#, and have to go through a mental translation process. Let alone Linq... Thanks Richard.
Richard Deeming 9-Oct-19 6:48am
   
Same here - I haven't done any serious VB.NET work for over 15 years. :)
gacar 10-Oct-19 7:24am
   
This is! Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100