Filter out based on date

I have a dt nameD DtConversationDT that contains a column called FirstCreatedATAsDate(datatype -system.dateTime).I need to filter out all the rows where time is less than 4 hors from current time using Linq .
I tried the below Linq but getting error as "Source contains no Data rows

DtConversationDT.AsEnumerable() _
.Where(Function(row) row.Field(Of DateTime)(“FirstCreatedATAsDate”).AddHours( CInt(in_Config(“PendingTime”).ToString)) >= DateTime.UtcNow) _
.CopyToDataTable()

Hi @tharani.natarajan

Try this way. It might work.

currentTime As DateTime = DateTime.UtcNow

thresholdTime As DateTime = currentTime.AddHours(-4) 

filteredRows = From row In DtConversationDT.AsEnumerable()
                   Let conversationTime = row.Field(Of DateTime)("FirstCreatedATAsDate")
                   Where conversationTime >= thresholdTime
                   Select row

If filteredRows.Any() 
Then
   filteredDataTable As DataTable = filteredRows.CopyToDataTable()
    ' Use the filteredDataTable as needed
Else
    ' Handle the case when no rows match the criteria

Hope it helps!!

Hi @tharani.natarajan

Try this

DtConversationDT.AsEnumerable() _
.Where(Function(row) row.Field(Of DateTime)("FirstCreatedATAsDate") >= DateTime.UtcNow.AddHours(-CInt(in_Config("PendingTime").ToString))) _
.CopyToDataTable()
1 Like

getting error as “Source contains no Data rows”

@tharani.natarajan

Once check your data table contains same column or not

Hi, the error “Source contains no Data rows” indicates that there might not be any rows in your DtConversationDT DataTable that satisfy the condition. Once verify whether your DataTable contains data or not.

I have rows

I have the required column

You are getting this error because no row is left after filtering datatable. For clarification, do you want to keep all the rows having time of more than now-4 hours (basically meaning processing all records of last 4 hours) or do you want all rows which are less than now-4 hours (meaning older than now-4 hours)?

i need to take the records that are older than 4 hours from current time and add to to a datatable

In this case try below

DtConversationDT.AsEnumerable() _
.Where(Function(row) row.Field(Of DateTime)("FirstCreatedATAsDate") < DateTime.UtcNow.AddHours(-CInt(in_Config("PendingTime").ToString))) _
.CopyToDataTable()

I have copied pasted and modify the solution given by @lrtetala

You are right ,Thanks

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