string - 如何使用 Join 函数从结构列表中序列化特定的字符串属性?
问题描述
我有一个自定义结构列表,我想将一个特定属性序列化为一个字符串,但我不知道在这种情况下如何正确使用 Join 函数?
Structure structPerson
Dim Name As String
Dim Age As Integer
End Structure
Sub Main()
Dim p1 As structPerson = New structPerson() With {.Name = "John", .Age = 20}
Dim p2 As structPerson = New structPerson() With {.Name = "Samantha", .Age = 12}
Dim persons As List(Of structPerson) = New List(Of structPerson) From {p1, p2}
Dim strNames As String = persons.Join(", ", Function(p) p.Name) 'HOW TO JOIN NAMES OF ALL PERSONS IN LIST INTO ONE STRING?
Debug.WriteLine(strNames)
End Sub
我想使用 Join 函数得到字符串“John, Samantha”。
谢谢你的建议。
#JK
解决方案
Dim strNames As String = String.Join(", ", persons.Select(Function(p) p.Name))
String.Join
这是您用来将 numtipleStrings
与对之间的分隔符连接在一起的共享方法。它将加入Strings
将转换为的任何可枚举列表或对象Strings
。在这种情况下,您使用 LINQ 函数从对象Select
列表中获取Name
值列表。
推荐阅读
- javascript - 试图弄清楚如何根据特定书籍的流派来更改背景图像
- java - 参数索引超出范围(1 > 参数个数,即 0)尝试读取 SQL 表时
- c# - PartialView 未在 Asp.net MVC 中显示?
- php - parse_url() 在 PHPUnit 和普通 PHP 文件中的行为不同
- c - 双向链表 - 合并排序后更新 list->tail
- c# - 不同平台上的NaCL密封盒
- python - 验证正则表达式:最小/最大字符、一个连字符和开始/结束条件
- c++ - LZW 解码的 C++ 问题
- android - 在开发模式下的应用程序中创建了奇怪的帐户
- reactjs - 必需的属性和类型电子邮件不起作用。ReactJS 和 Materia UI