LINQ Query Dynamic Column

dt.AsEnumerable.Where(function(row) cdate(row(“Column0”).ToString).Day = 5 and cdate(row(“Column0”).ToString).Month = 5).CopyToDataTable

In this query I am only able to check for Column0 i.e the first column of my excel sheet But I want to check for all the columns in my excel sheet. Can somebody modify this Query for Me ?

Hi,

How about the following?

dt.AsEnumerable.Where(function(row) dt.Columns.Cast(Of DataColumn).Any(Function(c) cdate(row(c).ToString).Day = 5 AndAlso cdate(row(c).ToString).Month = 5)).CopyToDataTable

Regards,

As an alternate we can use the itemArray from the row

(From d in dtData.AsEnumerable
Let rchk = d.ItemArray.Select(Function (x) CDate(x.toString.trim)).Any(Function (d) d.Day = 5 AndAlso d.Month = 5)
Where rchk
Select r = d).CopyToDataTable

In case we do expect empty filter result we can handle as described here:
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

This Gives an error

Assign: Conversion from string “ABC” to type ‘Date’ is not valid.

Hi,

Can you try the following if your data contains non-date string?

 dt.AsEnumerable.Where(function(row) dt.Columns.Cast(Of DataColumn).Any(Function(c) IsDate(row(c).ToString) AndAlso cdate(row(c).ToString).Day = 5 AndAlso cdate(row(c).ToString).Month = 5)).CopyToDataTable

Regards,

One last Question

The Day and Month is not constant here it changes everyday.

Like If today is 5th May it will fetch 5th May records
Then tomorrow it should auto increment itself by one and fetch the records of 6th May

It says Columns is not a member of string

Hi,

How about the following expression?

dt.AsEnumerable.Where(function(row) dt.Columns.Cast(Of DataColumn).Any(Function(c) IsDate(row(c.ColumnName).ToString) AndAlso cdate(row(c.ColumnName).ToString).Day = Today.Day AndAlso cdate(row(c.ColumnName).ToString).Month = Today.Month)).CopyToDataTable

Regards,

And What if I want to

Fetch 6th may records on 5th May
And 7th may records on 6th May

Hi,

The following will work. Can you try this?

dt.AsEnumerable.Where(function(row) dt.Columns.Cast(Of DataColumn).Any(Function(c) IsDate(row(c.ColumnName).ToString) AndAlso cdate(row(c.ColumnName).ToString).Day = Today.AddDays(1).Day AndAlso cdate(row(c.ColumnName).ToString).Month = Today.AddDays(1).Month)).CopyToDataTable

Regards,

Perfect Thanks For this

1 Like

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