Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining StringBuilder

I am a newbie

I have problem with string builder. I want to show to Richtextbox in vb with Richtextbox template

i.e.

Jan 674  Meet 670  Missed 4
Feb 635 Meet 631 Missed 4

etc.

with source from datagirdview with 8 columns and xxxx rows.

for ex. columns are : Registered Date, Deadline Date, Month, Meet/Not Meet,etc.

This my Code :

For Each keyvalue As KeyValuePair(Of String, Integer) In DicMonth
 sb.AppendLine(String.Format("{0} : {1}", UCase(keyvalue.Key), keyvalue.Value))
Next

For Each keyvalue1 As KeyValuePair(Of String, Integer) In DicMeetTotal
  sb.AppendLine(String.Format("{0}", "MEET : " & keyvalue1.Value))
Next

RichTextBox2.Text = sb.ToString

and the result is :

Jan : 674
Feb : 635
Mar : 623
Meet : 670
Meet : 631
Meet : 621
Missed : 4
Missed : 4
Missed : 2
like image 802
Kukuh Sp Avatar asked Feb 28 '26 22:02

Kukuh Sp


1 Answers

Assuming the same order and length of dictionaries, you can use Zip to stitch the two dictionaries together:

Sub Main
    Dim sb = New StringBuilder()
    Dim DicMonth = New Dictionary(Of String, Integer)() From { _
        {"Jan", 674}, _
        {"Feb", 635} _
    }
    Dim DicMeetTotal = New Dictionary(Of String, Integer)() From { _
        {"Jan", 670}, _
        {"Feb", 631} _
    }

    Dim lineStrings = DicMonth.Zip(DicMeetTotal, _
       Function(m, mt) String.Format("{0} {1} Meet {2} Missed {3}", _
           m.Key, m.Value, mt.Value, m.Value - mt.Value))
    For Each ls In lineStrings
        sb.AppendLine(ls)
    Next
    Console.WriteLine(sb.ToString())
End Sub

Alternatively, if there is a join key (e.g. the Key value in both dictionaries is the same), you can use Linq Join them together, like so:

Dim lineStrings = DicMonth.Join(DicMeetTotal, _
     Function(m) m.Key, Function(mt) mt.Key,  _
     Function(m, mt) String.Format("{0} {1} Meet {2} Missed {3}", _
        m.Key, m.Value, mt.Value, m.Value - mt.Value))

Edit

Assuming that you wouldn't have modelled N different dictionaries each containing just a single value (this would be a modelling error along the lines of Entity Attribute Value, IMO), I'm guessing you'll want an entity to hold the data:

Class MeetTotalEntity
   Public Property Meet As Integer
   Public Property Missed As Integer
   Public Property Cancel As Integer
   Public Property Other As Integer
End Class

And then the Zip (or Join) still holds. The Value of the second dictionary contains the above entity, so just dereference the fields accordingly.

Sub Main
     Dim sb = New StringBuilder()
        Dim DicMonth = New Dictionary(Of String, Integer)() From { _
            {"Jan", 674}, _
            {"Feb", 635} _
        }
        Dim DicMeetTotal = New Dictionary(Of String, MeetTotalEntity)() From { _
            {"Jan", New MeetTotalEntity With {.Meet = 670, .Missed = 4, .Cancel = 10, .Other = 5}}, _
            {"Feb", New MeetTotalEntity With {.Meet = 631, .Missed = 10, .Cancel = 3, .Other = 2}} _
        }

        Dim lineStrings = DicMonth.Zip(DicMeetTotal, _
           Function(m, mt) String.Format("{0} Total {1} Meet {2} Missed {3} Cancel {4} Other {5}", _
               m.Key, m.Value, mt.Value.Meet, mt.Value.Missed, mt.Value.Cancel, mt.Value.Other))
        For Each ls In lineStrings
            sb.AppendLine(ls)
        Next
        Console.WriteLine(sb.ToString())
End Sub
like image 117
StuartLC Avatar answered Mar 04 '26 06:03

StuartLC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!