我有一个字符串'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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句