Expression: Converting date format to string in Expression

Hi team,

If this condition is full filled I want to reload data into the data table
IF
dt_Test60days.AsEnumerable().Where(Function(x) CDate(x(“Start Dt”).ToString()).Date >= New Date(Today.Year, Today.Month, 2) AndAlso CDate(x(“Start Dt”).ToString()).Date <= New Date(Today.Year, Today.Month, 2).AddDays(59)).ToString.Count>0

Reload to data table
dt_Test60days.AsEnumerable().Where(Function(x) CDate(x(“Start Dt”).ToString()).Date >= New Date(Today.Year, Today.Month, 2) AndAlso CDate(x(“Start Dt”).ToString()).Date <= New Date(Today.Year, Today.Month, 2).AddDays(59)).CopyToDataTable()

However, I receive the following error,

Can anyone look into this? Thanks a lot!

Hi @Sisay_Dinku

Please try this & let us know:

If dt_Test60days.AsEnumerable().Where(Function(x) DateTime.Parse(x("Start Dt").ToString()).Date >= New Date(Today.Year, Today.Month, 2) AndAlso DateTime.Parse(x("Start Dt").ToString()).Date <= New Date(Today.Year, Today.Month, 2).AddDays(59)).Count > 0 Then
    dt_Test60days.AsEnumerable().Where(Function(x) DateTime.Parse(x("Start Dt").ToString()).Date >= New Date(Today.Year, Today.Month, 2) AndAlso DateTime.Parse(x("Start Dt").ToString()).Date <= New Date(Today.Year, Today.Month, 2).AddDays(59)).CopyToDataTable()
End If

Hope this helps,
Best Regards.

Hi @Sisay_Dinku ,

The Error is because there is mostly Empty value in the Start Dt column.

To Handle this, we can perform a initial check and if it exists only we can perform the rest of the checks and get the filtered data.

Also, We an reduce the re-write of the expression by using a Data Row Array type variable :

  1. Create a data row array variable, say dr_Array.
  2. Next, use the Expression in the below way :
dr_Array = dt_Test60days.AsEnumerable().Where(Function(x)Not String.IsNullOrWhiteSpace(x("Start Dt").ToString) andAlso CDate(x("Start Dt").ToString()).Date >= New Date(Today.Year, Today.Month, 2) AndAlso CDate(x("Start Dt").ToString()).Date <= New Date(Today.Year, Today.Month, 2).AddDays(59)).ToArray
  1. Next, Use an If Condition to check if the dr_Array contains any data. We can do it using the below Expression :
dr_Array.Any
  1. Next, In the Then part of the If you can use the below Expression :
dt_Test60days = dr_Array.CopyToDatatable

In the Else Part, You can add the clone :

dt_Test60days = dt_Test60days.Clone

Thus we should be able to avoid the re-writing of the expression.

1 Like

@arjunshenoy

I receive the following error this time

→ String was not recognized as a valid DateTime.