Set the date with LINQ

I’m triying to assign the actual date to the empty values using LINQ. I have an assign activity with this code but I’m confused with the structure of the if condition.

I have this code:

LHS: dt | DataType: DataTable
RHS: dt.AsEnumerable()Where(Function(row), If(row(“Date”).Equals(“”),DateTime.Now.ToString(“dd/MM/yyyy”)))

Thnks

@ShadowZ

Have a look here for LINQ starter help:

Your LINQ is conflicting with some discussed topics:

  • A where LAMBDA requires Boolean as LAMBDA output
  • A LINQ Where operator (when not regulated further) will have an output of 0,1 or more items
  • general syntax issues

It looks like you are trying to check in a datatable for empty date values. But it is missmatching following specifics:

  • return todays date string, when all rows do contain a blank Date column value
  • retrun todays date string, when row has a blank Date column value

what is needed?

I just want to set the actual date to the values that are in the column date with an empty value. My code is wrong becuase something is missing.

Ok, let me repeat your requirement, so you can check if it was undestood.

You are trying to use LINQ within an assign activity for updating all Date column values, which are blank with todays date.

If so: This is against the nature of LINQ as offered within the capabilites of the Assign activity

Have a look here discussing several options:
How to Update Data Column Values of a Data Table | Community Blog

kindly note also the Invoke code approach. But the given LINQ is wrong in syntax and will not work, in taht way

Hey @ShadowZ!! Look at this solution! In my case my input DT is this.

  • I created a datatable to store the final DT with the empty values ​​updated with “Today”. (dt_result)

  • I’ll skip the reading parts.

  • I then make a copy of the structure of my input dt to my result dt.

  • I do the update query.

(From d In dt_input.AsEnumerable
Let ra= New Object(){
d(0),
	If(String.IsNullOrEmpty(d(1).toString),
	d(1),
	DateTime.Now.ToString(“dd/MM/yyyy”))
}
Select dt_result.Rows.Add(ra)).CopyToDataTable
  • So I write this new DT.

Hope it helps!

Hi,
There are a lot of ways to do it, for example and update can be one, but this link sorry but don’t help me because there isn’t any example of an if condition case with LINQ.
Thnks.

you cannot expect the detailed code lines matching your case within the BLOG. But the mentioned

  • filter with LINQ
  • Update the filtered rows
    is matching your case

2.1:

we gave you the feedback that your targeted approach is not recommended and the tried syntax is against the LINQ nature used within an assign.

passing the filtered rows to the for each activity | typeargument: datarow
frow| values:
dtData.AsEnumerable.Where(Function (x) isNothing(x("Date")) orElse String.IsNullOrEmpty(x("Date").toString.Trim))

within the for each, we do update:
frow(“Date”) = now.toString(“dd/MM/yyyy”)

1 Like

Hi,

Okey, I’m trying to do it and works fine but I have a question.
In my Excel the column date is on the E position, and I’m triying to set the same code but changing the numbers, because I want to set the date on the column E too.
I’m using this number position:

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

Thnks.

Okey, let my try with this code.

Wow, works nice. I tried with empty values and anothers with info, and respect the values with info and set the values in blank with the actual date.

Thanks man, you save me :star_struck:

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