我正在向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强制转换为ListItem 。请帮助。
答案 0 :(得分: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;
}
...同样地
希望有所帮助
答案 1 :(得分: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
为空或为空,则两个表都相同