感谢您提出先进的想法。我不确定是否有办法,但值得一试。
您将在下面看到我要引用的代码。我创建的代码效果很好;但是,当循环复制到 dtPeople
时, t.Field<string>("Source")
中有多个值(以逗号分隔并在引号中) .我需要遍历这些值,在该行上创建一个具有相同信息的新行,只是会有一个不同的 t.Field<string>("Source")
.
DataTable dtPeople =
(from t in tempDtUsers.AsEnumerable()
where t.Field<string>("role").ToLower() == "user"
select dtPeople.LoadDataRow(new object[]
{
Id,
t.Field<string>("SourcedIds").Substring(6),
t.Field<string>("Source"),
t.Field<string>("FName"),
"",
t.Field<string>("LName"),
t.Field<string>("Email")
}, false)).CopyToDataTable();
它如何写入 DT 的示例:
"999999","060","100110257","Billy",,"Bob","bb@test.org"
"999999","168","101912217,100110265","Joe",,"Shmo","js@test.org"
需要它像这样出来:
"999999","060","100110257","Billy",,"Bob","bb@test.org"
"999999","168","100110265","Joe",,"Shmo","js@test.org"
"999999","168","101912217","Joe",,"Shmo","js@test.org"
您可以使用 SelectMany
到项目Source
分成多个项目:
var dtPeople = new DataTable();
tempDtUsers.AsEnumerable()
.Where(t => t.Field<string>("role").ToLower() == "user")
.Select(t => new
{
Id,
SourcedIds = t.Field<string>("SourcedIds").Substring(6),
Source = t.Field<string>("Source"),
FName = t.Field<string>("FName"),
MName = "",
LName = t.Field<string>("LName"),
Email = t.Field<string>("Email")
})
.SelectMany(x => x.Source.Split(',').Select(source => dtPeople.LoadDataRow(new[]
{
x.Id,
x.SourcedIds,
source,
x.FName,
x.MName,
x.LName,
x.Email
}, false)))
.CopyToDataTable();
编辑:CopyToDataTable 需要 IEnumerable<T> where T: DataRow
仅有的。我以为T
可以是任何类...
我是一名优秀的程序员,十分优秀!