日期:2014-05-17  浏览次数:20886 次

Entity framework String 和 DateTime的对比

from u in abc 
where Convert.ToDateTime(u.Times) > DateTime.Now
select u

这个Convert.ToDateTime报错
LINQ to Entities 不识别方法“System.DateTime ToDateTime(System.String)”,因此该方法无法转换为存储表达式。
我用的是MySql的数据库
由于mysql表中没有直接和datetime匹配的类型我就使用了string类型的数据类型
而且entity framework对mysql貌似支持很少。。。
这个怎么解决

------解决方案--------------------
from u in abc.AsEnumerable() 
where Convert.ToDateTime(u.Times) > DateTime.Now
select u
------解决方案--------------------
无论什么问题,一劳永逸最简单的解决办法就是自定义一个函数

参考
http://hi.baidu.com/meteortent/item/e07656b1fb8743d284dd798e

只要你会写对应的sql,就没有解决不了的。
------解决方案--------------------
当然你也可以试试这个方法: SqlFunctions.DateDiff 

http://msdn.microsoft.com/en-us/library/dd466270(v=vs.110).aspx
------解决方案--------------------
引用:
from u in abc.AsEnumerable() 
where Convert.ToDateTime(u.Times) > DateTime.Now
select u


一行就能让服务器崩溃
------解决方案--------------------
where System.Data.Objects.SqlClient.SqlFunctions.DateDiff("s",DateTime.Now,u.Time)>0

where System.Data.Objects.EntityFunctions.DiffSeconds(DateTime.Now,u.Time)>0
跟踪生成的sql语句