DateParse

Hello everyone!
The LINQ query converts the date from the format “MM/dd/yyyy 00:00:00” to “dd.MM.yyyy”.
It was: 02/12/2022 00:00:00.
It became: 12.02.2022.
But it throws an error if the date cell is empty. Tell me, please, how to make it so that there is no error? Thanks.

dtSample.AsEnumerable().ToList().ForEach(Sub(row) row(“DateCol”)=DateTime.ParseExact(row(“DateCol”).ToString,“MM/dd/yyyy 00:00:00”,System .Globalization.CultureInfo.InvariantCulture).ToString(“dd.MM.yyyy”))

Hi @sereganator,

Before changing the date type, use an if condition and check if the incoming value is null or not.
Expression: String.IsNullOrEmpty(YourVariable) or String.IsNullOrWhiteSpace(YourVariable)

If true, then the value is empty
If false, in else block use this conversion expression.

I hope this would help you to solve your issue.

Regards,
@90s_Developer

1 Like

Is it possible to check for emptiness in the same LINQ query?

Hey @sereganator!! Lets go to the solution!

(From d In dt_input.AsEnumerable
Let ra= New Object(){
d(0),
	If(String.IsNullOrEmpty(d(1).toString),
	d(1),
	DateTime.ParseExact(d(1).ToString,“MM/dd/yyyy”,System .Globalization.CultureInfo.InvariantCulture).ToString(“dd.MM.yyyy”))
}
Select dt_result.Rows.Add(ra)).CopyToDataTable

Hope it helps!!

Thanks. Is it possible to convert the current LINQ query to TryParse?
dtSample.AsEnumerable().ToList().ForEach(Sub(row) row(“DateCol”)=DateTime.ParseExact(row(“DateCol”).ToString,“MM/dd/yyyy 00:00:00”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd.MM.yyyy”))

Are you hard coding it or passing in assign activity?

I want to understand how it can be done through TryParse, it seems to me more elegant.
I tried to convert the code, but it doesn’t compile.
dtSample.AsEnumerable().ToList().ForEach(Sub(row) row(“DateCol”)=DateTime.TryParseExact(row(“DateCol”).ToString,“MM/dd/yyyy 00:00:00”,System.Globalization.CultureInfo.InvariantCulture,DateTimeStyles.None).ToString(“dd.MM.yyyy”))

TryParseExact has an additional parameter within the method signature for the returned date, so set this to nothing when not using it or use it

in your usage it is grabing the True/False returned from tryParseExact and will set it as datecol column value. We do assume that this was not your intention

1 Like

Absolutely right, I just don’t fully understand how the TryParse method works.

If, for example, the string must not be empty and date format, if so, then we parse, otherwise we do nothing. Wrote an example code, but with errors.
dtSample = dtSample.AsEnumerable.Select(Function(r)
r(0) = If (r(0) IsNot Nothing AndAlso DateTime.DateTime.ParseExact(r(0).ToString,“MM/dd/yyyy 00:00:00”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd.MM.yyyy”)),
Return r
End Function).CopyToDataTable

let us share some information resources with you, so you explore for training purpose

as you want to update a datacolumn value - find the right fitting approach

Get an intro to LINQ

see what can be done with LINQ

get access to the official docu

Now, the main building blocks are cleared and you can start with the implementation once you decided for the best fitting approach.

We would not recommend to use LINQ-Invoke Code constructs as long there is strong reason for this. As we can use other options as well

1 Like

As @ppr said, using invoke code in these cases is not the best approach. Take a look at the solution I sent above based on @ppr 's own LINQ teachings.