Filtering a Datatable based on Date

Hi, I want to filter a datable with 4 colums, one of it is “Date”. The format of the date is: dd.MM.yyyy (I don’t know if this is important or not). I want to only keep the rows where the date in colum “Date” is not older than 5 years. reference date today.

Research DataTable.Select(filter)

(From x in yourDT.AsEnumerable
Where Today.AddYears(-5) < Datetime.ParseExact(x.Item(“Date”).ToString, “dd.MM.yyyy”, System.Globalization.CultureInfo.InvariantCulture)
).CopyToDataTable

4 Likes

Hallo Dongjin, your code works, thanks you for that. Can you explain parts of your code, so I can understand what your did there ?

I have problems understand the logic and syntax of this part

To compare 2 dates, you need to change the type of your data to Datetime.

I used Datetime.ParseExact(String, String, IFormatProvider) method so you’d better check this page

It converts String to Datetime

Very important that you write code that you can understand . Not now, but when you get back to it 6 months from now. Or , if you need to change something when functionality changes in the future.

Also, you might want to research efficiency of Linq especially about garbage collection.

1 Like

Exactly as you said. I always try to learn the code, otherwise it’s just brainless copy and paste and I would never make any progress. Normally I understand the code of others by myself, but what Dongjin posted is beyond my capabilities and knowledge.

1 Like

Thanks, I will look into it.

Hi @B.D

It is very gracious of @Dongjin_Lee to post what he has learned and practiced for sometime. I come from C# background and started using VB.NET once again only after I got into UiPath. I used to write LINQ and Lambda functions to solve most of my problems until I moved into a team that did not know LINQ and weren’t interested in supporting a solution given its nature.

This is also another factor that must be considered when developing solutions in a democratic team environment. One member of the team might be great at doing something, but that may not always work for others. :frowning:

I’m glad you see both perspectives. thanks for your honest feedback :slight_smile:

1 Like

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