dr = (From row In dt.AsEnumerable Where row("Column1").ToString.Trim.Equals("X") And
Convert.ToDateTime(row("Column2")) > Convert.ToDateTime(row(Column3)) Select row)
The issue you’re encountering is due to the FormatException while converting the date columns in your LINQ query. This typically happens when the date strings are not in a recognized format.
Try using this LINQ which will check for the format.
dr = (From row In dt.AsEnumerable()
Let dateColumn2 = TryParseDate(row("Column2").ToString().Trim())
Let dateColumn3 = TryParseDate(row("Column3").ToString().Trim())
Where row("Column1").ToString().Trim().Equals("X") AndAlso
dateColumn2.HasValue AndAlso dateColumn3.HasValue AndAlso
dateColumn2.Value > dateColumn3.Value
Select row).ToList()
I’ve checked the data that I’m using and looks like the query performs the second condition of the Where statement even if the first condition is not met. Those rows with no ‘X’ values are the ones with blank column 2 or 3. Using AndAlso, instead of And, fixed the issue.