c# - 使用拆分将字符串列表转换为对象列表
问题描述
我有一个字符串“obAttachmentTypes”列表,其中每个字符串如下所示:(ID|NAME|LEVEL)。我正在尝试将这些字符串转换为 AttachmentType 对象列表,以便更易于管理。我使用 .Select 函数来翻译字符串列表,但我在尝试拆分字符串时遇到了麻烦。我已经研究过使用 Split() 函数,但我不确定在这种情况下如何使用它。
List<KeywordDataSetItem> obAttachmentTypes = GetAttachmentTypes(app);
var attachTypes = obAttachmentTypes
.Select(at => new AttachmentType
{
//use split here?
AttachmentTypeId = at.AlphaNumericValue.Split(''),
AttachmentTypeName = at.AlphaNumericValue.Split(''),
IsPopular = true,
AttachmentTypeLevel = at.AlphaNumericValue.Split(''),
})
.ToList();
return attachTypes;
解决方案
您需要先围绕管道符号拆分字符串,然后从结果数组中的不同索引中获取字符串。
var attachTypes = obAttachmentTypes
.Select(at => at.AlphaNumericValue.Split('|'))
.Select(arr => new AttachmentType
{
AttachmentTypeId = arr[0],
AttachmentTypeName = arr[1],
IsPopular = true,
AttachmentTypeLevel = arr[2],
})
.ToList();
推荐阅读
- python - 从 api 请求中获取模型对象
- scala - 如果一个数据结构是可折叠的,它是一个幺半群吗?
- angularjs - 在angularjs中的子指令中访问父范围
- plot - 如何在 Octave 中设置用户定义的颜色图?
- ios - 升级 SDK 后,使用 Facebook 登录在 iOS 11 上没有响应
- c# - 解析具有不同内容的 JSON 响应
- ios - 如何从 url 制作模型、视图模型
- android - 如何在 Android 中使用 SQLcipher 数据库保护 Greendao 使用的密码?
- git - 如何找到将文件更改为当前状态的 Git 提交?
- dialogflow-es - 对话流 Web 挂钩超时