-3

I would like to check if a datatable value exists in the second datatable which will return true or false.

 DataTable table1;
 DataTable table2;

if Any of table1 rows is in table2 then return true

akd
  • 6,538
  • 16
  • 70
  • 112
  • Just showing us `DataTable table1 and DataTable table2` are not good enough.. please tell me that you have more code than this..? show some more effort on your end please.. – MethodMan Oct 23 '14 at 15:26
  • `Jon Skeet` has an awesome example assuming you understand `Linq or Lambda's` http://stackoverflow.com/questions/164144/compare-two-datatables-to-determine-rows-in-one-but-not-the-other – MethodMan Oct 23 '14 at 15:28
  • How do you plan to determine if two rows are equal? Do you have a column that, if matches, means the rows are the same? Or do you need to compare the value of each column to determine equality? – Rufus L Oct 23 '14 at 15:37
  • Both tables have ID columns. it should iterate to check if one id is in the another table or not it will compare each row from one table to another. if there is even one match the result will be true. if there is no match then the result is false. – akd Oct 23 '14 at 15:40

1 Answers1

0

You can use LINQ To DataSet to achieve that like:

bool matchFound =table1.AsEnumerable()
                        .Select(row1 => row1.Field<int>("ID"))
                    .Intersect(
                        table2.AsEnumerable()
                        .Select(row2 => row2.Field<int>("ID)))
                        .Any();
user2711965
  • 1,795
  • 2
  • 14
  • 34