我是个新手
我对字符串生成器有问题。我想用Richtextbox模板在vb中显示Richtextbox
即
Jan 674 Meet 670 Missed 4
Feb 635 Meet 631 Missed 4等。
数据源来自8列xxxx行的datagirdview。
对于ex。列有:注册日期、截止日期、月份、见面/不见面等。
这是我的代码:
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结果是:
Jan : 674
Feb : 635
Mar : 623
Meet : 670
Meet : 631
Meet : 621
Missed : 4
Missed : 4
Missed : 2发布于 2015-02-04 13:08:50
假设字典的顺序和长度相同,您可以使用Zip将两个字典缝合在一起:
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或者,如果存在连接键(例如,两个字典中的键值相同),您可以将它们放在一起使用Linq Join,如下所示:
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))编辑
假设您没有建模N个不同的字典,每个字典只包含一个值(这将是一个沿着实体属性值的线的建模错误,IMO),我猜您会想要一个实体来保存数据:
Class MeetTotalEntity
Public Property Meet As Integer
Public Property Missed As Integer
Public Property Cancel As Integer
Public Property Other As Integer
End Class然后Zip (或Join)仍然有效。第二个字典的Value包含上述实体,因此只需相应地取消对字段的引用。
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 Subhttps://stackoverflow.com/questions/28313698
复制相似问题