How to compare two DataSet columns values in C#? -


in below code want compare 2 dataset column's values not match getting true condition.so how compare?

if (dsemp.tables[0].columns["empname"].tostring() == dsalltables.tables[2].columns["empname"].tostring()) {  } 

you comparing 2 column-names, "empname" "empname" true. tables[0].columns["empname"] returns datacolumn name , tostring returns name of column "empname". that's pointless.

if instead want know if 2 tables contain same empname value in 1 of rows can use linq:

var emprowsempname = dsemp.tables[0].asenumerable().select(r => r.field<string>("empname")); var allrowsempname = dsalltables.tables[2].asenumerable().select(r => r.field<string>("empname")); ienumerable<string> allintersectingempnames = emprowsempname.intersect(allrowsempname); if (allintersectingempnames.any()) {  } 

now know empname values contained in both tables. use foreach-loop:

foreach(string empname in allintersectingempnames)     console.writeline(empname); 

if want find out if specific value contained in both:

bool containsname = allintersectingempnames.contains("samplename"); 

if want first matching:

string firstintersectingempname = allintersectingempnames.firstordefault();  if(firstintersectingempname != null){      //  yes, there @ least 1 empname in both tables } 

Comments