How to get row Closest to 12:00 PM without looping

Hi All,

I have a extracted datatable with one column which is time slots. from this time slots data. I want to retrieve time slots/row which is closes to 12:00 PM

For eg.

05:00 AM
07:00 AM
09:00 AM
05:00 PM
09:00 PM

So the output should be 9:00 AM since its closest to 12:00 PM

I was think of linq where I subtract the rows using cdate(“Column1”).hour - 12 and then get the min using min function but I have never used min function and I even have to retrieve value as the original data only


Can you try the following?

dt.AsEnumerable.OrderBy(Function(r) Math.Abs((DateTime.Parse(r(0).ToString)-DateTime.Parse(target)).TotalHours)).First.item(0)

Main.xaml (7.1 KB)


1 Like