检测列中的多个值。然后循环遍历这些值

时间:2018-02-02 16:46:01

标签: c# linq

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

下面你会看到我要提到的代码。我创建的代码很有用;但是,在循环复制到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"

1 个答案:

答案 0 :(得分:4)

您可以使用plt.ylabel(r'$\int\limits_{\scriptscriptstyle 0}^{\scriptstyle x} \frac{du}{1+u^2}$', size=12) SelectMany投影到多个项目中:

Source

编辑:CopyToDataTable仅需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(); 。我以为IEnumerable<T> where T: DataRow可以是任何一个班级......