Select(find) max date from datatable


#1

I have a datatable with a few strings of dates:
9/30/2018 00:00:00
1/6/2018 00:00:00
2/15/2018 00:00:00
2/28/2018 00:00:00
3/15/2018 00:00:00
3/15/2018 00:00:00
12/15/2017 00:00:00
12/15/2017 00:00:00
12/15/2017 00:00:00
3/8/2018 00:00:00
2/28/2018 00:00:00
2/28/2018 00:00:00

I need to find max date value from this datatable.

Can some one help me to achieve this?

Thanks in advance
Alexey


#2

Hi there @Alexey,
You can use the following expression:
[YourDTVariable].AsEnumerable().Max(Function (drRows) DateTime.Parse(drRows.Item([YourColumnIndex/Name]).ToString)).ToString

This will return a String value.

On a side note, you will receive an error in the event any of these values cannot be parsed as a DateTime.

Thanks,
Josh


#3

Mr_JDavey,
Thank you!!!
It works!!


#4

Hi there @Alexey,
I just had another play around with this, it may be worth incorporating something like the below:
[YourDTVariable].AsEnumerable().Max(Function (drRows) If(DateTime.TryParse(drRows.Item([YourColumnIndex/Name]).ToString, Nothing), DateTime.Parse(drRows.Item([YourColumnIndex/Name).ToString), DateTime.MinValue)).ToString

This means it will check each of the elements within your DataTable, should it find one that cannot be parsed, it will treat it as a minimum DateTime value, otherwise it will be parsed and evaluated accordingly.

Just for future reference :slight_smile:

Thanks once again,
Josh


#5

@Mr_JDavey - Nice info . Do you have any solution if i need to take the entire row associated with that max column? As @Alexey mentioned i have the same column along with many columns. for e.g Name,company,ID,joined date. I need to get the entire row value of Name,Company,ID and Joined date based on the max joined date. Possible?


#6

Hi there @kk.virags,
Definitely, I will post an example a little later.

Thanks in advance,
Josh


#7

what is drRows here??