将数据集分成匹配和不匹配的数据集 [英] divide dataset in to matched and unmatched datatables
问题描述
在我的项目中有两个数据集 dsNames
和 dsDetails
。
In my project there are two datasets dsNames
and dsDetails
.
我不了解SQL。这里我连接到XML和Textfiles。
I dont know about SQL. Here I am connecting to XML and Textfiles.
dtNames
EmployeeName EmployeeRole
a 2
b 3
c 4
dtDetails
Empid EmployeeName EmpCity EmployeeRole
101 a abc 3 //EmpRole not equal to above table EmpRole
102 b abc 3
103 c xyz 4
104 d pqr 5
105 e rst 6
我想将这两个数据集与 EmployeeName
和 EmployeeRole
(这里比较 dsNames
),并将结果存储在另一个DataSet dsMain
(来自 dsDetails
的表),然后根据比较,将两个数据集分成 mat chedDataTable
和 unmatchedDataTable
。
I want to relate these two datasets based on the EmployeeName
and EmployeeRole
(here comparing to dsNames
) and store the result in another DataSet dsMain
(table from dsDetails
) and then divide the two dataSets in according to comparison like matchedDataTable
and unmatchedDataTable
.
我知道这可以使用DataRelation 或 RowFilter ,但我不能得到一个想法如何做到这一点,因为有两列要与其他datatable两列进行比较我不知道(我是初学者在.net)
I know this can be done using DataRelation or RowFilter but i cant get a thought how to do this because there are two columns to be compared with other datatable two columns which i dont know.(I am beginner in .net)
我试过以下代码:(不工作)
foreach (DataRow rwMain in dsNames.Tables[0].Rows)
{
foreach (DataRow rwSub in dsDetails.Tables[0].Rows)
{
if (rwMain["EmployeeName"].Equals(rwSub["EmployeeName"]))
{
if (rwMain["EmployeeRole"].Equals(rwSub["EmployeeRole"]))
{
// Matched DataTable
}
else
{
//Unmatched DataTable
}
}
}
}
我在上面的代码中做错了,而且看起来我没有以正确的方式做到这一点。
I am doing something wrong in the above code and also it looks me i am not doing it in a right way.
请不要linq因为我知道,但我不能在我的项目中使用它。
Please no linq because i know it but i cant use it in my project.
请帮助。
推荐答案
我通过使用选择
Property
I solved it by using Select
Property
DataRow[] matchedRows = dtNames.Select("EmployeeName = '" + dsDetails.Tables[0].Columns[0].ToString() + "'"
+ " AND " + " EmployeeRole ='" + dsDetails.Tables[0].Columns[1].ToString() + "'");
//
DataRow[] mismatchedRows = dsDetails.Tables[0].Select("EmployeeName <> '" + dtNames.Columns[0].ToString() + "'"
+ " OR" + " EmployeeRole <>'" + dtNames.Columns[1].ToString() + "'");
这篇关于将数据集分成匹配和不匹配的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!