I’m building a dynamic query in my ASP.NET MVC project by the following:
Dim queryString As String = "SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos"
If strWhere <> "" Then
queryString = queryString & " WHERE " & strWhere
End If
' Call the constructor with the specified query and the ObjectContext.
Dim SearchUsersQuery As New System.Data.Objects.ObjectQuery(Of UserInformation)(queryString, MyDB)
Dim lstOfUsers As List(Of UserInformation) = SearchUsersQuery.ToList
Where basically the strWhere is a string that I build up a dynamic filter depending on what the user selects to search on.
This works great until I need to add a date comparison to the date clause.
I’m trying the following:
strWhere = " userInfos.BirthDate <= " & StartDateForQuery.ToShortDateString
Which will end up as:
"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos Where userInfos.BirthDate <= 10/09/1992"
But when i try to execute the query with the ToList whenever a date is in the where string i get the following error:
The argument types ‘Edm.DateTime’ and ‘Edm.Int32’ are incompatible for this operation. Near WHERE predicate, line 1, column 103.
Any ideas on what my issue is?
Thanks in advance
Why don’t you use the next code:
You make use of the linq capabilities to generate queries (it “knows” how to generate the datetime parameter for DB query).
Check a sample here