2

我正在向 DataSet 添加两个表,我想检查它们是否相等。

我试过的代码是:

SqlDataAdapter mydat = new SqlDataAdapter("Select Device_Profile_Param+'='+Device_Profile_Default_Value AS SettingsCheck From Device_Profile_Master Where Device_Profile_Name = '" + Label5.Text + "'", con); 
DataTable dt = new DataTable(); 
mydat.Fill(dt);

DataSet dset = new DataSet();
dset.Tables.Add(dt);

SqlDataAdapter mydata = new SqlDataAdapter("Select Device_Profile_Param+'='+Device_Profile_Default_Value AS Settings From Device_Profile_Master Where Device_Profile_Name = '" + For_Profile_Num.Items[i] + "'", con);
DataTable dt2 = new DataTable();
mydata.Fill(dt2);

dset.Tables.Add(dt2);

var hashSet1 = new HashSet<string>(dset.Tables[0].Rows.Cast<ListItem>().Select(x => x.Value));
var hashSet2 = new HashSet<string>(dset.Tables[1].Rows.Cast<ListItem>().Select(x => x.Value));

var result = hashSet1.SetEquals(hashSet2);
if (result == true)
{
found = 1;
}

我收到一个错误DataSet cannot be cast as ListItem。请帮忙。

4

2 回答 2

0

使用IEnumerable如下来比较两个表...

IEnumerable<string> table1Value= dset.Tables[0].AsEnumerable().Select(x => (string)x[ColumnName]); 

IEnumerable<string> table2Value= dset.Tables[1].AsEnumerable().Select(x => (string)x[ColumnName]); 

IEnumerable<string> ChangedValue = table2Value.Except(table1Value); 

如果ChangedValued为 null 或为空,则两个表相同

于 2012-09-13T09:42:22.947 回答
0

在将某些列名添加到 HashSet 时定义它...

System.Collections.Hashtable myHashtable1 = new System.Collections.Hashtable();
                myHashtable.Add(dset.Tables[0].Rows[0]["Column1"], myDataTable1.Rows[0]["Column2"]);
                myHashtable.Add(dset.Tables[0].Rows[1]["Column1"], myDataTable1.Rows[1]["Column2"]);

System.Collections.Hashtable myHashtable2 = new System.Collections.Hashtable();
                myHashtable.Add(dset.Tables[1].Rows[0]["Column1"], myDataTable1.Rows[0]["Column2"]);
                myHashtable.Add(dset.Tables[1].Rows[1]["Column1"], myDataTable1.Rows[1]["Column2"]);


var result = myHashtable2 .SetEquals(myHashtable1);
if (result == true)
{
found = 1;
}

同样地...

希望能帮助到你

于 2012-09-13T09:16:19.973 回答