Dynamic Expression Casting DateTime as timestamp

最后都变了- 提交于 2019-12-24 15:05:39

问题


When running a raw linq statement against entities with entity framework 5 - db2400 database, this where clause is rendered as good SQL and executes as needed:

where entity.Number == stringNumber && entity.EffectiveDate == effectiveDate

Rendered SQL Clause:

WHERE (Filter1.NUMBER = @p__linq__0) AND (Filter1.EFFECTIVE_DATE = @p__linq__1)}

However, when dynamically generating an expression with this same DateTime comparison, the SQL is sent to the DB2400 as a string casted timestamp. And DB2 says, "I cannot compare a timestamp and a Date!" (since the column is of type Date on the table).

Rendered Expression:

Entity => ((Entity.Number == "somestring") And (Entity.EffectiveDate == 10/1/2012 12:00:00 AM))

Rendered SQL:

WHERE (('someString' = Extent1.NUMBER)) AND (CAST('2012-10-01 00:00:00.00000' AS timestamp) = Extent6.EFFECTIVE_DATE)}

How is this DateTime expression built?

Expression.Equal(Property, DateTime.Parse(someDateTime.ToString()));

来源:https://stackoverflow.com/questions/21048053/dynamic-expression-casting-datetime-as-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!