Selecting Latest Year and Latest Month, and Oldest Year and Oldest Month(LINQ)

I have the following DataTable
image

My goal is to get the Latest Year:Latest Month , Oldest Year:Oldest Month

{“2021:3” , “2019:1”}

Is it possible to achieve this using LINQ?
Output can be in any format, Array would be most preferable.

Thanks in Advance!

Kind Regards,
Ashwin A.K

Hi,

Can you try the following expression?

Oldest

dt.AsEnumerable.OrderBy(Function(r) new DateTime(Int32.Parse(r("Year").ToString),Int32.Parse(r("Month").ToString),1)).Select(Function(r) r("Year").ToString+":"+r("Month").ToString).First()

Latest

dt.AsEnumerable.OrderBy(Function(r) new DateTime(Int32.Parse(r("Year").ToString),Int32.Parse(r("Month").ToString),1)).Select(Function(r) r("Year").ToString+":"+r("Month").ToString).Last()

Regards,

1 Like

Thank you @Yoichi-san!
Brilliant solutions as always!

Kind Regards,
Ashwin A.K

1 Like

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