Linq Query to filter datatable Date Column

Hello,

I have a datatable with multiple columns but i want o filter it on one column which has dates in it. See below for an example of what the column looks like
Join Date
12/23/2015
5/26/2017
11/5/2020
7/27/2015
9/28/2015
3/13/2019
5/6/2014
8/26/2016

I want to filter the datatable based on this column where the date is higher than another date for xample where it is higher than 04/23/2017

@hatakora

create Variables:

  • dtFiltered | DataType: DataTAble
  • filterDate = new DateTime(2017,4,23)
  • Result = DataType: List(Of DataRows)

Assign activity
Left: Result|
Right

(From d in dtData.AsEnumerable
Let dp = CDate(d(“YourColName”).toString.Trim).Date
Where dp > filterDate.Date
Select r = d).toList

To avoid Exceptions thrown by CopyToDataTable for empty filter result we returned the List of Datarows.

Use an If Activity:
Condition: Result.Count > 0
Then: dtFiltered = Result.CopyToDataTAble
Else: dtFiltered = dtData.Clone

Assumption. Dates are within DataTable in format we can use with CDate and are not blank.
Otherwise we would adopt the date conversion accordingly

@ppr worked like a charm. I actually just did Select r = d).CopyToDatatTable so i don’t have to add to a list first.
Thank you very much

Perfect. May we ask you to mark the solving post as solution. So others can benefit form it. Thanks

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.