How to use try catch in singgle line vb?

i have code

dtData.AsEnumerable.Where(Function(x) DateTime.ParseExact(x(2).ToString,"dd/MM/yyy",System.Globalization.CultureInfo.InvariantCulture) < Now )

If there is data in column 2 that doesn’t match the format, it will error, I’m thinking of giving it a try catch for the date conversion, is that possible?

Thanks for your help

You could use TryParseExact. Note that you might get more readable code by running a For Each Row in Data Table. But, if you want to do it in a Linq statement, you could try the following:
(a DateTime variable dateResult should be declared for it to work).

(From x In dtData.AsEnumerable() _
Let canParse = DateTime.TryParseExact(x(2).ToString,"dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, Nothing, dateResult) _
Where canParse AndAlso dateResult < Now
Let result = dateResult
Select result).ToList

Can you please explain me the mean of “Let canParse = DateTime.TryParseExact(x(2).ToString,“dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture, Nothing, dateResult) _”

because i get error “Name ‘dateResult’ is either not declared or not in the current scope.”

You need to declare the variable for the code to work.

Statement looks like a filtering. We assume that you are interested on a returned datatable

dtFiltered =

(From d In YourDataTable.AsEnumerable()
Let sd = d("YourColNameOrIndex").toString.Trim
Let pc = DateTime.TryParseExact(sd,"YourDateFormat", CultureInfo.InvariantCulture, DateTimeStyles.None, nothing) 
Where pc
Let dp = DateTime.ParseExact(sd,"YourDateFormat", CultureInfo.InvariantCulture)
Where dp.Date < Now.Date
Select r = d).CopyToDataTable

Ensure:
import_systemglobalization

Handling empty results
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

within an Assign the resulting Variable (dateResult) from TryParseExact is not forwarded and therefore not available.

The code I provided runs fine on my machine and provides expected results. Not sure why, but it seems to work differently in this case, compared to just calling TryParseExact directly.

thanks for the feedback:

can you share with us details:

  • ProjectCompatibility
  • screenshot from the modelling
  • input datatable screenshot from immeditate panel
  • result data screenshot from immediate panel

Sure thing.

1 Like

@efelantti
good catch. Thanks for digging out

So lets sort it out:

Yes is needed

has to be scoped to Legacy

Details

grafik
grafik

But looks good in Windows we can confirm and replicated:

can confirm for both: Legacy, Windows. Looks like the Assign is not wiring tryParseXX with the variable within Assign (loosing the reference) - at least in Leagacy the Invoke Code is doing it

@Iwan_Kurniawan2 sorry for blowing up the topic withthis technical clearing. Just let us know, what output you are expecting from the filter LINQ and we will guide to the end

1 Like

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