首页 新闻 赞助 找找看

EF查询问题

0
悬赏园豆:10 [已解决问题] 解决于 2018-05-29 18:28

我想查询当前时间这个小时内的数据,请问该怎样查? 我这样写报错。

var list=  db.user.Where( x =>x.UserName == userName && x.DateTime.ToString("yyyyMMddhhmmss") == DateTime.Now.ToString("yyyyMMddhhmmss")).ToList();

 

“System.NotSupportedException”类型的异常在 EntityFramework.SqlServer.dll 中发生,但未在用户代码中进行处理

其他信息: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

正在缓冲的主页 正在缓冲 | 初学一级 | 园豆:154
提问于:2015-04-20 16:39
< >
分享
最佳答案
0
var list=  db.user.Where( x =>x.UserName == userName && x.DateTime >= DateTime.Now.AddHours(-1)).ToList();
收获园豆:10
dudu | 高人七级 |园豆:31075 | 2015-04-20 16:51

还是报错

“System.NotSupportedException”类型的异常在 EntityFramework.SqlServer.dll 中发生,但未在用户代码中进行处理

其他信息: LINQ to Entities does not recognize the method 'System.DateTime AddHours(Double)' method, and this method cannot be translated into a store expression.

正在缓冲 | 园豆:154 (初学一级) | 2015-04-20 16:55

@正在缓冲: 改为这样:

var oneHourAgo = DateTime.Now.AddHours(-1);
var list=  db.user.Where( x =>x.UserName == userName && x.DateTime >= oneHourAgo).ToList();
dudu | 园豆:31075 (高人七级) | 2015-04-20 17:00
其他回答(2)
0

var list=  db.user.Where( x =>x.UserName == userName && x.DateTime > DateTime.Now.AddHours(-1)).ToList();

刘宏玺 | 园豆:14020 (专家六级) | 2015-04-20 16:50

还是报错

“System.NotSupportedException”类型的异常在 EntityFramework.SqlServer.dll 中发生,但未在用户代码中进行处理

其他信息: LINQ to Entities does not recognize the method 'System.DateTime AddHours(Double)' method, and this method cannot be translated into a store expression.

支持(0) 反对(0) 正在缓冲 | 园豆:154 (初学一级) | 2015-04-20 16:55

@正在缓冲: 楼下已给出正确答案

支持(0) 反对(0) 刘宏玺 | 园豆:14020 (专家六级) | 2015-04-20 18:58
0

大哥,你查的是当前这个小时内的数据吗?仔细检查一下你的代码勒

| 园豆:780 (小虾三级) | 2015-04-20 22:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册