【问题标题】:how to copy a row of a dataset into another dataset? [duplicate]如何将数据集的一行复制到另一个数据集中? [复制]
【发布时间】:2015-08-23 18:08:24
【问题描述】:

我正在使用 2 个循环遍历数据集,试图找到其 ID 与分配给数组的值匹配的行,如果它们匹配,我想将该行复制到另一个表中。例如:

DataSet dsWinners = new DataSet();
for(int i =0;i<=TotalWinners;i++)
{
    for (int j = 1; j <= ds.Tables[0].Rows.Count; j++)
    {
        //this is my ds table 0
        left = Convert.ToInt16(ds.Tables[0].Rows[i]["ID"]);
        //this is my array 
        right = Convert.ToInt16(Winners[i, 0]);

        if ( left == right )//if array value matechs with ds.table[0] ID
        {
            dsWinners.Tables[0].Rows[i] = ds.Tables[0].Rows[j];
        }
    }
}

我如何获取记录/行并将其复制到一个新表中,如果我不能像这样复制该行,是否有替代方法?

【问题讨论】:

    标签: c#


    【解决方案1】:
        DataTable tempDt = new DataTable();
        tempDt = ds.Tables[0].Clone();
        ds.Tables[0].Rows.OfType<DataRow>().ToList().ForEach(x =>
        {
            int rowIndex = ds.Tables[0].Rows.IndexOf(x);
            if (rowIndex < TotalWinners && 
                Convert.ToInt16(x["ID"]) == Convert.ToInt16(Winners[rowIndex, 0]))
            {
                tempDt.ImportRow(x);
            }
        });
        DataSet dsWinners = new DataSet();
        dsWinners.Tables.Add(tempDt);
    

    编辑:

        Dictionary<string, string> winnersList = new Dictionary<string, string>();
        for (int i = 0; i < TotalWinners; i++)
        {
            winnersList.Add(Winners[i, 0], Winners[i, 1]);
        }
        string idList = string.Join("','", winnersList.Select(x => x.Key));
        DataSet dsWinners = new DataSet();
        dsWinners.Tables.Add(ds.Tables[0].Select("ID IN ('" + idList + "')").CopyToDataTable());
        dsWinners.Tables[0].Columns.Add("prize_amt", typeof(string));
        dsWinners.Tables[0].Rows.OfType<DataRow>().ToList().ForEach(x =>
        {
            x["prize_amt"] = winnersList[x["ID"].ToString()];
        });
        dsWinners.Tables[0].AcceptChanges();
    

    希望这会有所帮助...

    【讨论】:

    • @Ali 这个怎么样?
    • 我得到“索引超出了数组的范围。”在 if 行!!!
    • @Ali 这个编辑过的怎么样?现在假设TotalWinners 是数组中的元素数
    • 它的工作,但我得到的结果是错误的。我只得到 1 行!
    • @Ali 你能试试我编辑的答案吗...
    【解决方案2】:
    DataSet dsWinners = new DataSet();
    DataTable dataTable= ds.Tables[0].Clone();
    
    for(int i =0;i<=TotalWinners;i++)
      {
           for (int j = 1; j <= ds.Tables[0].Rows.Count; j++)
           {
              //this is my ds table 0
              left = Convert.ToInt16(ds.Tables[0].Rows[i]["ID"]);
              //this is my array 
              right = Convert.ToInt16(Winners[i, 0]);
    
              if ( left == right )//if array value matechs with ds.table[0] ID
              {
                //  dsWinners.Tables[0].Rows[i] = ds.Tables[0].Rows[j];
                  dataTable.Rows.Add( ds.Tables[0].Rows[j]);
               }
          }
      }
    dsWinners.Add(dataTable);
    

    【讨论】:

    • 解决后我得到以下错误:该行已经属于另一个表。
    • 把最后一行改成dsWinners.Tables.Add(dataTable);
    • dataTable.Rows.Add(ds.Tables[0].Rows[j].ItemArray); // 它会解决你的问题,[链接] (stackoverflow.com/questions/4020270/…)
    【解决方案3】:
    DataSet ds = new DataSet();
    
    DataTable dt = new DataTable("ExampleTable");
    dt.Columns.Add(new DataColumn("Age",typeof(int)));
    dt.Columns.Add(new DataColumn("Name", typeof(string)));
    
    DataRow dr = dt.NewRow();
    dr["Age"] = 30;
    dr["Name"] = "Mike";
    dt.Rows.Add(dr);
    ds.Tables.Add(dt);
    

    【讨论】:

    • 我的数据列每次都不同...我无法手动添加它们
    【解决方案4】:

    如果我正确地阅读了您的问题,那么您需要在满足特定条件时从一个数据集中复制行并将其转移到另一个数据集。假设两个数据集具有相同的结构,此示例就足够了。

          DataSet DSResult = new DataSet();
            DSResult.Tables.Add();
    
            DSResult.Tables[0].Columns.Add("ID", typeof(int));
            DSResult.Tables[0].Columns.Add("Name", typeof(string));
    
            DSResult.Tables[0].Rows.Add(1,"Jon");
            DSResult.Tables[0].Rows.Add(2, "Kyle");
            DSResult.Tables[0].Rows.Add(3, "Sam");
            DSResult.Tables[0].Rows.Add(4, "Peter");
            DSResult.Tables[0].Rows.Add(5, "Lily");
    
    
            DataSet DSWinners = new DataSet();
            DSWinners.Tables.Add();
            DSWinners = DSResult.Clone();
    
            int[] Id = new int[] { 1, 4, 5 }; //condition to match
    
            foreach (int val in Id)
            {
                DataRow[] Samplerow =
                  DSResult.Tables[0].AsEnumerable()
                 .Select((row, index) => new { row, index })
                 .Where(item => item.row.Field<int>("ID") == val)
                 .Select(item => item.row)
                 .ToArray();
                    DSWinners.Tables[0].ImportRow(Samplerow[0]);
    
                // If both tables are not same then
                   string YourVal=Samplerow[0]["ID"].ToString();
                    DSWinners.Tables[0].Rows.Add();
                    DSWinners.Tables[0].Rows[0]["YourColumnname"]=Yourval //Should have same Datataype
    
    
            }
    

    【讨论】:

    • 你说得对。但有一个问题。两个数据集的结构不同,因为一个数据填充了所有数据,第二个是新数据集
    • 我的数据列每次都不同...我无法手动添加它们
    猜你喜欢
    • 2013-07-26
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多