EnumerableRowCollection<DataRow> { !<FormatException> ... } value of variable

Hi All,

Any idea why I’m getting this value for my EnumerableRowCollection variable?

EnumerableRowCollection<DataRow> { !<FormatException> ... }

The expression goes like this:

dr = (From row In dt.AsEnumerable Where row("Column1").ToString.Trim.Equals("X") And
	Convert.ToDateTime(row("Column2")) > Convert.ToDateTime(row(Column3)) Select row)

@_pjflo

As per error column2 or 3 is having an invalid value other than date

May be a blank or any other value which could jot be parsed

Cheers

@_pjflo,

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()

Thanks,
Ashok :slight_smile:

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.

Thanks a lot for your help, @Anil_G !

1 Like

@_pjflo

Glad you were able to solve the issue

Yes its most of the times better to use andAlso as well as OrElse instead of And and Or

Cheers

Yeah, I realized that now. Thanks again! :smile:

1 Like

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