[英]How to Compare Two Datatable and get common records in other DataTable in C#?
I have Two DataTable
with Two columns in each Table
. 我有两个
DataTable
,每个Table
有两列。 Now, i want to Compare these Two DataTable
and the Matching rows in Third DataTable
in C#. 现在,我想比较这两个
DataTable
和C#中第三个DataTable
中的匹配行。
Eg: 例如:
DataTableA
ColA1 ColA2
1 sampletext1
2 sampletext2
4 sampletext4
DataTableB
ColB1 ColB2
1 sampletext1
2 sampletext2
3 sampletext3
DataTableC
ColC1 ColC2
1 sampletext1
2 sampletext2
I have tried it using for loop but it slows down. 我已经尝试过使用for循环,但是速度变慢。 Any other alternative.
任何其他选择。
Use like below. 如下使用。 It will work faster
它会更快地工作
var matched = from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable() on table1.Field<string>("sno") equals table2.Field<string>("sno")
where table1.Field<string>("name") == table2.Field<string>("name")
select table1;
if (matched.Count()>0)
{
DataTable dtt = matched.CopyToDataTable();
}
Dont forget to mention as answer if it helps. 如果有帮助,别忘了提及作为答案。
for(i=0;i<dt1.rows.count;i++)
{
if (dt2.rows.count > i)
{
if((dt1.rows[i][1] == dt2.rows[i][1]) && (dt1.rows[i][2] == dt2.rows[i][2]))
{
dt3.rows.add(dt.rows[i])
}
}
}
First use merge function merge two datatable, and finnaly for loop on datatable and find result by select function: 首先使用合并功能合并两个数据表,最后对数据表进行循环并通过选择函数查找结果:
dt1 = dt.Copy();
dt1.Merge(dt2);
//Here match condition third Data table
for(int i=0; i <=dt1.rows; i++)
{
DataRow[] foundRows = dt3.Select("page_name='" + dt1.[i]['colname'] + "'");
//function or code here;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.