在《LINQ Cookbook》的第5个条目(拼接CheckedListBox中选中的字符串)中,微软公司Visual Basic团队演示了分别使用两种VB 9中的新特性来实现这类字符串连接的方法。
\在第一种方法里,Visual Basic团队使用了LINQ的聚合(Aggregate)语法。如下述代码所示,将各个字符串用逗号连接起来:
\MsgBox( _
\tAggregate Box In CheckedListBox1.CheckedItems _
\tInto Concat())
代码中的Concat是一个扩展方法(extension method),其实现如下:
\Public Module AggregateModule
\tPublic Function Concat(Of Type)( _
\t\tByVal ie As IEnumerable(Of Type)) As String
\t\tDim str As String = \"\"
\t\tFor Each item In ie
\t\t\tIf str \u0026lt;\u0026gt; \"\" Then str \u0026amp;= \
本文通过《LINQCookbook》中的一个实例,展示了如何使用Visual Basic 9中的LINQ特性和Aggregate语法来拼接CheckedListBox中选中的字符串。通过具体的代码示例介绍了Aggregate方法及自定义Concat扩展方法的实现。
6638

被折叠的 条评论
为什么被折叠?



