在 WHERE 子句中执行带有日期的 SELECT 语句失败
我正在我的 ASP.NET MVC 项目中通过以下方式构建动态查询:
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
其中 strWhere 基本上是一个字符串,我根据用户选择搜索的内容构建动态过滤器。
这非常有效,直到我需要向日期子句添加日期比较。
我正在尝试以下操作:
strWhere = " userInfos.BirthDate <= " & StartDateForQuery.ToShortDateString
最终结果为:
"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos Where userInfos.BirthDate <= 10/09/1992"
但是当我尝试使用 ToList 执行查询时,只要日期位于 where 字符串中,我就会收到以下错误:
参数类型“Edm.DateTime”和“Edm.Int32”与此操作不兼容。靠近 WHERE 谓词,第 1 行,第 103 列。
对我的问题有什么想法吗?
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能以这种方式比较日期。
当您执行此操作时:
10/09/1992
将被解释为Int
值。尝试在该值周围加上单引号,如下所示:
可能您必须调用数据库日期转换函数(取决于您的数据库供应商)将此日期字符串传递给它。
像这样:
问题是这个查询被发送到数据库并且是执行它的数据库服务器。这就是为什么您需要一个数据库特定的日期转换函数。
例如:在 Oracle 中,我们有 to_date 函数,可以使用给定模式将字符串转换为日期时间:
在 SQL Server 中,我们有 < code>convert 函数如下:
更多示例此处。
You can't compare dates that way.
When you do this:
10/09/1992
is being interpreted as anInt
value.Try putting single quotes around this value as in:
Probably you'll have to call a database date conversion function (depends on your database vendor) passing to it this date string.
Like this:
The problem is that this query is being sent to the database and is the database server that'll execute it. That's why you need a database specific date conversion function.
For example: in Oracle we have the to_date function to convert a string to a datetime using a given pattern:
In SQL Server we have the
convert
function as in:More samples here.
为什么不使用下一个代码:
您利用 linq 功能来生成查询(它“知道”如何为数据库查询生成日期时间参数)。
此处查看示例
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