gpt4 book ai didi

c# - 检测列中的多个值。然后遍历这些值

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:50 25 4
gpt4 key购买 nike

感谢您提出先进的想法。我不确定是否有办法,但值得一试。

您将在下面看到我要引用的代码。我创建的代码效果很好;但是,当循环复制到 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可以是任何类...

关于c# - 检测列中的多个值。然后遍历这些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48587261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com