Click here to Skip to main content
15,890,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Dictionary
Dim DicInt As New Dictionary(Of Integer, List(Of Integer))
DicInt.Add(5, {4, 5, 3}.ToList)
DicInt.Add(6, {8, 1}.ToList)

How can i get string like this
5 > 4,5,3
6 > 8,1  

Thanks.

What I have tried:

Dim strSutun2 As String = String.Join(vbNewLine, String.Join(" > ", DicInt.Select(Function(list) String.Join(",", list))))
Posted
Updated 10-Dec-19 12:32pm
v3
Comments
[no name] 15-Nov-19 11:06am    
A foreach loop on the dictionary key would be easier (to understand).

LINQ isn't going to be "faster".
gacar 15-Nov-19 11:24am    
Contrary to popular belief, the linq method is not very slow. At least from what I saw in my own programs. It is also tempting that the code is simple.
phil.o 15-Nov-19 11:40am    
What is the outcome of the code snippet you provided?
gacar 15-Nov-19 11:48am    
I had this: [5, System.Collections.Generic.List`1[System.Int32]] > [6, System.Collections.Generic.List`1[System.Int32]]
phil.o 15-Nov-19 12:04pm    
Why not trying to create an extension method to a Dictionary(of T, List(of U)) first? Then, if you really want to use Linq for that, you could try to translate it.

Here you are for the C# version.

C#
using System.Collections.Generic;
using System.Text;

// You need a static class for extension methods.
public static class DictionaryExtensions
{
   public static string Dump<T, U>(this IDictionary<T, IList<U>> dictionary)
   {
      StringBuilder builder = new StringBuilder();
      string newline = Environment.NewLine;
      int length = newline.Length;
      foreach (var pair in dictionary)
      {
         builder.AppendFormat
         (
            "{0} > {1}",
            pair.Key,
            string.Join(",", pair.Value)
         );
         builder.Append(Environment.NewLine);
      }
      builder.Remove(builder.Length - length, length); // Remove last line feed
      return builder.ToString();
   }
}

Usage:
VB.NET
Dim strSutun2 As String = DicInt.Dump()

Hope this helps.
 
Share this answer
 
Comments
gacar 15-Nov-19 13:59pm    
Thank you for your solution.
gacar 15-Nov-19 14:12pm    
I converted code. But i couldnt call. How can i call in vb?

Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.CompilerServices
Module DictionaryExtensions
<extension()>
Function Dump(Of T, U)(ByVal dictionary As IDictionary(Of T, IList(Of U))) As String
Dim builder As StringBuilder = New StringBuilder()
Dim newline As String = Environment.NewLine
Dim length As Integer = newline.Length

For Each pair In dictionary
builder.AppendFormat("{0} > {1}", pair.Key, String.Join(",", pair.Value))
builder.Append(Environment.NewLine)
Next

builder.Remove(builder.Length - length, length)
Return builder.ToString()
End Function
End Module
gacar 15-Nov-19 14:14pm    
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim DicInt As New Dictionary(Of Integer, List(Of Integer))
DicInt.Add(5, {4, 5, 3}.ToList)
DicInt.Add(6, {8, 1}.ToList)

Dim strSutun2x As String = DicInt.Dump() 'Here error occured
End Sub
phil.o 15-Nov-19 14:34pm    
Have you tried Imports DictionaryExtensions at the beginning of your form code?
gacar 15-Nov-19 14:45pm    
I imported but i have this error still :
"Error BC30456 'Dump' is not a member of 'Dictionary(Of Integer, List(Of Integer))'.
Try this:
VB.NET
Dim DicInt As New Dictionary(Of Integer, List(Of Integer))
DicInt.Add(5, New List(Of Integer) From {4, 5, 3})
DicInt.Add(6, New List(Of Integer) From {8, 1})


Dim result = DicInt _
    .Select(Function(kvp) _
        String.Concat(kvp.Key, " => ", _
            String.Join(", ", kvp.Value.Select(Function(x) x)))) _
    .ToList()
For Each s As String In result
	Console.WriteLine(s)
Next
 
Share this answer
 
Comments
gacar 15-Nov-19 19:26pm    
Thanks for solution.
Maciej Los 16-Nov-19 1:48am    
You're very welcome.
Maciej Los 18-Nov-19 5:17am    
Can you accept this answer as a solution? This should be done to remove your question from unanswered list. Use green button.
I found this solution.

VB
Dim str As String = String.Join(vbNewLine, DicInt.Select(Function(x) x.Key & " > " & String.Join(",", x.Value)))

Result:
5 > 4,5,3
6 > 8,1
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900