Error in Linq .Need Help

Hi All,

I am trying to extract the max date from a Data Table. I have rows of Invoices with multiple dates and I need to select the max date. I am trying to scrape the data and put in data table. from there I am using Linq to get the maximum dates. However I am getting error in Linq .

Convert.ToDateTime( ((From DataRow CurrentRow In ExtractDataTable_Invoice.Rows Orderby Convert.ToDateTime(CurrentRow[“Date”]) Descending Select CurrentRow).FirstOrDefault()[“Date]”))

image

image
I need to pick the latest date . 7/31/2022 and these dates can vary in run time however I need to pick up the latest dates

image

change format accordingly

Hi @dutta.marina

Sometimes the Convert.ToDateTime doesn’t Work correctly.

Can you try with the following expression, that might help you.

Datetime.ParseExact(“give your date here”,“M/dd/yyyy”,Gloabalization.CultureInfo.InvariantCulture)

Hi,

If you use C#, can you try the following?

Convert.ToDateTime( ((from DataRow CurrentRow in ExtractDataTable_Invoice.Rows orderby Convert.ToDateTime(CurrentRow["Date"]) descending select CurrentRow).FirstOrDefault()["Date"]))

Regards,

Hi Yoichi,

I am getting following error.

Hi @adiijaiin

I have used DateTime.TryParseExact( ((from DataRow CurrentRow in ExtractDataTable_Invoice.Rows orderby DateTime.TryParseExact(CurrentRow[“Date”]) descending select CurrentRow).FirstOrDefault()[“Date”])) but still getting error.

Hi Yoichi,

I tried


but still getting error

Hi @adiijaiin

I am getting error.

while using ParseExact you need to give the input format of the date as well.

Hi,

Can you share your workflow file if possible?
Basically it’s necessary cultureinfo and result argument if you use TryParseExact method.

Regards,

Hi Yoichi

Please find the file.Thank You
Date_Time.zip (3.3 KB)

Hi @adiijaiin

Is it something which I can write ? Let me know if this is correct. Attached my workflow also.
Date_Time.zip (3.3 KB)

ExtractDataTable_Invoice.AsEnumerable.Where(Function(r) DateTime.TryParseExact(r([“Date”]).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None,New DateTime)).Max(Function(r) DateTime.ParseExact(r([“Date”]).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None)).ToString(“MM-dd-yyyy”)

Hi,

We cannot open this project correctly. Can you check if can open this?

Regards,

@dutta.marina

Can you please confirm if yours is a c# or vb.net project…

Because i see a combination you are using in the code

Cheers

@Anil_G

For me its VB.Net

Hi @Yoichi

I have uploaded again. Can you check now.
Main.xaml (10.6 KB)

@dutta.marina

Then can you please try this please

Cdate(ExtractDataTable_Invoice.AsEnumerable.Where(Function(r) DateTime.TryParseExact(r(("Date")).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None,New DateTime)).Max(Function(r) DateTime.ParseExact(r("Date").ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None))("Date").ToString).ToString("MM-dd-yyyy")

Cheers

@Anil_G

I tried but getting error.


@dutta.marina

A small correction to the above

ExtractDataTable_Invoice.AsEnumerable.Where(Function(r) DateTime.TryParseExact(r(("Date")).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None,New DateTime)).Max(Function(r) DateTime.ParseExact(r("Date").ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None)).ToString("MM-dd-yyyy")

Cheers

@Anil_G

Thank You very much. Error has gone. But can you please explain the code ?Also why we are using the Function(r)?

Thanks once again