I'm trying to retrieve rows from a datatable where an integer exists within a list/string contained within one of the datatable columns.
for context, this is the number pertaining to the day of the week.
The column in the datatable is called 'runDays', and is a string which would look like this : 1,2,3,5 (meaning monday, tuesday, wednesday and friday in my region).
This is as close as I've got so far (my actual select statement is considerably more complex, but all working perfectly aside from this 'nowDay IN' bit, so I simplified it for the purposes of seeking assistance)...
Dim nowDay As Integer = CInt(DateTime.Now.DayOfWeek)
Dim result() As DataRow = dtbSchedule.Select(nowDay & " IN (runDays)")
I feel like I'm close, but not close enough - this generates an Evaluation exception...
System.Data.EvaluateException: Only constant expressions are allowed in the expression list for the IN operator.
Can anyone point me in the right direction for the correct syntax here?
Edit:
Dim result() As DataRow = dtbSchedule.Select("runType = 'weekly' AND '" & nowdate & "' >= startDate AND '" & nowdate & "' <= endDate AND runTime = '" & nowtime & "' AND " & nowDay & " IN (runDays)")
CodePudding user response:
The column in the datatable is called 'runDays', and is a string
Dim nowDay As String = CInt(DateTime.Now.DayOfWeek).ToString
Dim result() As DataRow
result = (From r In dtbSchedule.AsEnumerable
Where r("runDays").ToString.Contains(nowDay)
Select r).ToArray
EDIT: shows some of the other conditions. Should be able to figure out the rest. Note that I assumed some of the data types.
result = (From r In dtbSchedule.AsEnumerable
Where r("runType").ToString = "weekly" AndAlso
nowDate >= CDate(r("startDate")) AndAlso
r("runDays").ToString.Contains(nowDay)
Select r).ToArray