I would like to retrieve all the rows belonging to a datatable, whose values are found in another datatable.
As an example is worth 1000 words, here an example of what i want
I Want to keep rows From this DT :
Name
Number
Price
F1
200
1
F2
700
2
F3
200
3
F4
700
4
Where the column “Name” and “Number” match the column “ID” and “Number” of this one :
ID
Number
Colour
F2
700
Blue
F3
700
Green
F4
700
Purple
Result :
Name
Number
Price
F2
700
2
F4
700
4
I would like to have an efficient solution (if possible no for each activity, rather with a linq query)
So you can use LINQ to achieve this, but remember to add an If condition in case the result has no data it will throw an error, so for this create an if to check if the result >0
(From row1 In dt1.AsEnumerable()
Join row2 In dt2.AsEnumerable()
On row1("Name").ToString() Equals row2("ID").ToString() And row1("Number").ToString() Equals row2("Number").ToString()
Select row1).CopyToDataTable()
Same result can be achieved if you put the assign in a try catch activity.
Thank you all, I have the expected result! Simple curiosity, which is the most efficient method between that of Anil_G and Irtetala and why? Thanks again for the speed!
resultDt = (from row1 in dt1.AsEnumerable()
join row2 in dt2.AsEnumerable() on new { Name = row1.Field<string>("Name"), Number = row1.Field<double>("Number") }
equals new { Name = row2.Field<string>("ID"), Number = row2.Field<double>("Number") }
select row1).CopyToDataTable();