I want to filter common data rows based on some columns in below attatched example data tables. Help me to write a linq query or suggest other way.
First Datatable Image
Second Datatable Image
Note- Here i want to write a linq query which will give the matched result after comparing the columns of- ‘Start date’, ‘End date’ from First Datatable and ‘Date’ from second Datatable.
Anil_G
(Anil Gorthi)
May 22, 2024, 4:02am
2
@ankita.rai
Try this
Dt2.AsEnumerable.Where(function(x) dt1.AsEnumerable.Any(function(y) Cdate(y("Start Date").ToString) < Cdate(x("Date").Tostring) AndAlso Cdate(y("End Date").ToString) > Cdate(x("Date").Tostring))).CopyToDatatable
This checks if date is between strt and end date
If you need anything else change the conditions accordingly
Cheers
@ankita.rai Below are the sample file, used data table 1
Below is data from Table 2
var matchedDates = table2.AsEnumerable().Where(row2 =>
table1.AsEnumerable().Any(row1 =>
DateTime.Parse(row1["Start date"].ToString()) <= DateTime.Parse(row2["Date"].ToString()) &&
DateTime.Parse(row1["End date"].ToString()) >= DateTime.Parse(row2["Date"].ToString())));
Output we received
It return the 4th dep name
@ankita.rai I have attached the input file for my output.
datatable1.zip (372 Bytes)
@Anil_G , Thankyou for the quick reply, can you update the query to copy mismatched result?
ppr
(Peter Preuss)
May 22, 2024, 9:48am
5
Have a look on LINQ in general and get trained the main building blocks
[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum
Assign Activity:
dtUnmatched =
Dt2.AsEnumerable.Where(function(x) Not dt1.AsEnumerable.Any(function(y) Cdate(y("Start Date").ToString) < Cdate(x("Date").Tostring) AndAlso Cdate(y("End Date").ToString) > Cdate(x("Date").Tostring))).CopyToDatatable
we just denied the Any
system
(system)
Closed
May 25, 2024, 9:48am
6
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.