How to filter the current date using linq

Hi all,
How to filter the excel file by using linq query. In sample file having 2 columns Published date & Recent published date.
Here current date has to be filtered and store into another sheet. How to do this, please guide me on this.
test.xlsx (16.3 KB)

Hi @lakshmi.mp

Try this Linq:

filteredDt = (From row In dt 
Let PublishedDate = row("Published Date").ToString
Let RecentPublishedDate = row("Recent Published Date").ToString
Where PublishedDate.Contains(DateTime.Now.ToString("MMM-dd-yyyy")) AndAlso RecentPublishedDate.Contains(DateTime.Now.ToString("MMM-dd-yyyy"))
Select dt.Clone.Rows.Add(row.ItemArray)).CopyToDataTable()

filteredDt is of DataType System.Data.DataTable()

OUTPUT:

Hope it helps!!

2 Likes

@Parvathy , thanks a lot. It’s working…

1 Like

You’re welcome @lakshmi.mp

Happy Automation!!

1 Like

Hi @Parvathy ,
I tried executing the linq query, now getting error.


How to resolve this please guide me on this.

Hi @lakshmi.mp

If your datatable doesn’t contain todays data it will throw an error.

Does your datatable contain todays date.

Regards

@Parvathy , ya in current file there is no current date.
Need to handle this exception.

Hi @lakshmi.mp

Check the below workflow:

First Assign:

Bool_dt = If((From row In dt.AsEnumerable()
Where row("Published Date").ToString.Contains(DateTime.Now.ToString("MMM-dd-yyyy")) AndAlso 
      row("Recent Published Date").ToString.Contains(DateTime.Now.ToString("MMM-dd-yyyy"))
Select row).Count() = 0, True, False)

Bool_dt is of DataType System.Boolean

Assign inside in If:

filtered_dt = (From row In dt 
Where row("Published Date").ToString.Contains(DateTime.Now.ToString("MMM-dd-yyyy")) AndAlso row("Recent Published Date").ToString.Contains(DateTime.Now.ToString("MMM-dd-yyyy"))
Select row).CopyToDataTable()

filtered_dt is of DataType System.Data.DataTable()

Sequence2.xaml (10.3 KB)

Hope it helps!!

2 Likes

@Parvathy, thank you able to handle the exception.

1 Like

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