I need to sort a datatable based on a column whose value is in format mm/dd/yyyy hh:mi AM/Pm. Please help me with creating linq query for this
Please see if below post helps
Also, you can consider using sort datatable activity for same.
Hope this helps.
Regards
Sonali
Please try this
dt.AsEnumerable.OrderBy(function(x) Cdate(x("Datecolumnname").ToString)).CopyToDatatable
if above does not work use
dt.AsEnumerable.OrderBy(function(x) DateTime.ParseExact(x("Datecolumnname").ToString,"MM/dd/yyyy hh:mm tt",system.globalization.cultureinfo.invariantculture)).CopyToDatatable
cheers
You can use this linq
(From row In yourDataTable.AsEnumerable() Order By DateTime.ParseExact(row(“YourDateColumn”).ToString.Trim, “MM/dd/yyyy hh:mm tt”, System.Globalization.CultureInfo.InvariantCulture) Select row).CopyToDataTable()
If this is Needful then Mark it as SOLUTION
Happy Automation
Try this Linq query
sortedDT = (From row In yourDataTable.AsEnumerable()
Order By DateTime.ParseExact(row(“YourColumnName”).ToString.Trim,
“MM/dd/yyyy hh:mm tt”,
System.Globalization.CultureInfo.InvariantCulture)
Select row).CopyToDataTable()
Thanks!!
I tried this but it gives me error "string was not recognized as a valif datetime. The dates in table are in this format
11/2/2024 9:50 PM
4/24/2025 10:40 AM
The error happens because your DateTime.ParseExact format string doesn’t exactly match the way your date/time strings are stored.
From your examples 11/2/2024 9:50 PM
4/24/2025 10:40 AM
. These dates use single-digit months and days without leading zeros, so "MM/dd/yyyy hh:mm tt"
is too strict — MM
and dd
expect two digits.
You need to use M
and d
for single or double digits, like this:
(From row In yourDataTable.AsEnumerable()
Order By DateTime.ParseExact(
row(“YourDateColumn”).ToString.Trim,
“M/d/yyyy h:mm tt”,
System.Globalization.CultureInfo.InvariantCulture)
Select row).CopyToDataTable()
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.