Filtering Datarows out of a Datatable using select function

Hallo,
I want to filter data rows where the date is older than today.
for this I assigned: dtfilter = dtTest.Select.Where(function(x) x(“Date”).ToString < DateTime.Today.ToString(“dd.MM.yyyy”))

dtfilter as system.collections.ienumerable(system.data.datarow)

no error is shown, but in debugging mode it shows no entries.
After that I need to move the data in a diffrent datatable.

if you want to use string compare, you need to use String.Compare Method.
but string compare maybe give you wrong result

I’ll recommend datetime compare.
dtTest.Select.Where(function(x) DateTime.Parse(x(“Date”).ToString) < DateTime.Today).CopyToDataTable ← Assign Activity

CopyToDataTable transforms IEnumerable(DataRow) into DataTable.

Good Luck

It displays a conversion error.
what does “Datetime.Parse” mean ?

Hi @B.D instead of DateTime.Parse can u use Cdate(x(“Date”).ToString)

Regards

Nived N :robot:

Happy Automation :relaxed:

DateTime.Parse Method converts string to datetime.

1 Like

Hallo Nived, It says that it can’t convert string to date. Can you post the complete code ?

Hi @B.D

I think this will work

Cdate(x(“Date”))

1 Like

I can’t make it work

Show us how did you work and we can help you more precisely

Hi, it’s not a complicated process since it’s a test run.

dtTest has 2 Colums “ID” and “Date”. I then added couple of data rows.
Now I want to take all the entries out which are not from today.

for this I tried with assign action: dtfilter = dtTest.Select.Where(function(x) x(“Date”).ToString < DateTime.Today.ToString(“dd.MM.yyyy”))
And dtfilter as system.collections.ienumerable(system.data.datarow)

No error but also no result.

The last thing I tried is @Nived method:
dtfilter = dtTest.Select.Where(function(x) Cdate(x(“Date”)) < DateTime.Today.ToString(“dd.MM.yyyy”)).CopyToDataTable
dtfilter as DataTable

Beacuse Cdate(x(“Date”)) is type of Datetime, DateTime.Today will be OK.

dtfilter = dtTest.Select.Where(Fuction(x) Cdate(x(“Date”)) < Datetime.Today).CopyToDataTable

Plus, if there is no rows matching condition, CopyToDataTable method throws exception.

1 Like

Hi @B.D check this workflow

I did the same with the method i had specified and it is working fine.
Random_Numbers.zip (39.5 KB)

Check this workflow

Hope it helps you
Regards
Nived N
Happy Automation

1 Like

Hi Nived,
When I open your file it’s fine. But when I apply it to my workflow it says AsEnumerable is no member of System. Data.DataTable. I’m confused.
P.S maybe I should also mention I build the Datatable with Build Data Table activity

Hi @B.D check this link

1 Like

Nived you are awesome! thank you very much now it works :smiley:

My pleasure

Please mark solution so that we can close the topic :blush:

Hallo Nived, I have one last question. When I run the process it displays the following error:
Uipath
I tried to fix it, but I don’t know how without completely changing the function type

Did u tried with same code of mine ?

Yes, I used your code. The assign action shows no error anymore but output does.
This is the process:


Thisis the error message and the dtTest data entries at the right.
UipathPNG

Hi in the build datatable want datatype u had specified for date column is it string or datetime? @B.D