c# - Fill complex object property List based on string.split condition
问题描述
I have a complex object and need to fill the data based on split condition that I get from reader. Below object need to filter data based on "--" ( Example given below).
Class Employee {
public string fName {get;} {set;}
public List<string> Departments {get;} {set;}
}
once I get the data from reader, I am looping and one of the column Department has value like {"Sales -- Payroll -- HR"}
while (dr.read())
{
new Employee() {
fname = dr["name],
Departments = new List<string>
{
dr["Department"].ToString().Split(
new string[] {" -- "},
StringSplitOptions.None).ToString()
}
}
}
I tried above option but still not getting desired results.. I need 3 string array based on above example part of Departments property.
Any idea what I am missing?
解决方案
I think you mean to use the RemoveEmptyEntries
+ to make the result as ToList()
and assigned it.
Departments = dr["Department"].ToString().Split(new string[] {"--"}, StringSplitOptions.RemoveEmptyEntries).ToList()
In you're example you tried to make a Splitted array ToString()
which will give you a string representation of an object (and not an actual list object) and then you tried to put it in a list.
Instead, i've splitted the result into a list and assign it directly to Departments
and not to new List<string>
.
推荐阅读
- aframe - Oculus quest 2 控制器模型未在 A-Frame 中正确加载
- android - 当 sweepAngle 小于 360 度时,Android Studio Canvas.drawArc() 不绘制
- apache - Apache2 只是指向全局 documentRoot 而不是子域的文档根目录
- javascript - 从带有参数的函数返回 combineLatest 可观察对象
- adobe - 无法使用集成密钥,因为 Adobe Sign 中新生成的令牌的到期日期是上个月
- android - Android圆形按钮奇怪的差异
- javascript - 从JS中的日期字符串获取日期模式
- css - 绝对位置将搜索结果设置在另一个之上
- c - 检查两个字符串是否大小写相同
- java - 如何使用 junit 测试命令行应用程序