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?
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.”
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
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.
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