我循环遍历数据集,其中包含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];
}
}
}
如何获取记录/行并将其复制到新表中如果我不能像这样复制该行,是否有另一种方法可以做到这一点?
答案 0 :(得分:2)
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();
希望这会有所帮助......
答案 1 :(得分:0)
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);
答案 2 :(得分:0)
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);
答案 3 :(得分:0)
如果我正确阅读了您的问题,那么您需要在满足特定条件时从一个数据集中复制行并将其传输到其他数据集。假设两个数据集具有相同的结构,则此示例应该足够。
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
}