LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression 解决办法
根据用户输入的起始日期,查询以起始日期开始的前20条记录,在ASP.NET MVC的Controller代码中这样写:
var Logs = db.Log.Take(20);
if (!string.IsNullOrEmpty(dateBegin))
{
Logs = Logs.Where(a => a.Date >= Convert.ToDateTime(dateBegin)).Take();
}
运行后,出现下面错误信息:

对于这种情况,要清楚:本表达式只是LINQ to Entities,而不是真正的C#语言,虽然上述代码在编译是没有错误,但运行时,转换为SQL就产生了错误,无法转换为存储表达式。
解决办法是:将用户输入的起始日期的转换提前一步,使用真正的C#代码完成,然后将转换后的变量代入到LINQ表达式内,修改后的代码可以这样:
var Logs = db.Log.Take(20);
if (!string.IsNullOrEmpty(dateBegin))
{
DateTime dateB = Convert.ToDateTime(dateBegin);
Logs = Logs.Where(a => a.Date >= dateB).Take();
}
可以看到,首先将转换后的日期存入变量dateB内,然后再使用LINQ调用,不在LINQ中直接使用方法。
运行,正常。不再出现错误信息。
LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression 解决办法的更多相关文章
- 关于.ToList(): LINQ to Entities does not recognize the method ‘xxx’ method, and this method cannot be translated into a store expression.
LINQ to Entities works by translating LINQ queries to SQL queries, then executing the resulting quer ...
- 报错:System.NotSupportedException: LINQ to Entities does not recognize the method
报错:System.NotSupportedException: LINQ to Entities does not recognize the method ...... get_Item(Int3 ...
- LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法
一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...
- LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.
NormalSubmission=analysis.Count(x=>x.FinishTime<= endTime.AddDays(1))报错linq不能识别 => var endT ...
- LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method
System.Data.Objects.EntityFunctions和System.Data.Objects.SqlClient.SqlFunctions中的方法进行比较,如下 where Syst ...
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
if (!string.IsNullOrEmpty(FarmWorkId)) { data = data.Where(p => p.TypeId == Convert.ToInt32(FarmW ...
- 关于dubbo调度时出现Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method insertTestTb in the service cn.cuibusi.core.service.TestTbService.的解决办法
在用dubbo跨项目调度service时出现如下错误: 错误原因:pojo没有实现序列化 解决方法:在pojo实现序列化接口即可
- LINQ to Entities不支持Convert.ToDateTime方法解決一例
錯誤提示: LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' met ...
- LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new { MatNR = o.MatNR, ...
随机推荐
- CodeForces 687A NP-Hard Problem (二分图)
题意:给定 n 条边,然后让你把它分成两组,每组都有所有边的一个端点. 析:一开始我是先判定环,以为就不能成立,其实不是这样的,有环也行.用dfs进行搜索,并标记每一个端点,如果标记过并且和以前不一样 ...
- B-spline Curves 学习之B样条基函数计算实例(3)
B-spline Basis Functions: Computation Examples 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完 ...
- 测试嵌入GeoGebra网页
使用 http://ggbstudy.top/tools/ggb2html/ 将GGB文件免费托管,然后在博客内容中点击“HTML”按钮插入GGB网页地址: <iframe src=" ...
- [转]解决Mysql InnoDB: Failing assertion: ret || !assert_on_error问题
国庆回来后,发现mysql停止服务了,没办法继续启动了.查看日志,看到: 131008 09:56:03 mysqld_safe Starting mysqld daemon with databas ...
- PG数据库错误: 检测到OA幸运飞艇源码ShareLock死锁处理
PostgreSQL 是一个免费数据库,OA幸运飞艇源码下载,详情咨询[企娥166848365]对于处理分析型+交易型混合型系统来说确实很不错,特别是版本的升级到11.2后性能提升很多,很多运行机制跟 ...
- solr-DIH:dataimport增量全量创建索引
索引创建完毕,就要考虑怎么定时的去重建, 除了写solrj,可以定时调用下面两条url进行增量或者全量创建索引 全量:http://ip:port/webapp_name/core_name/da ...
- 查看JVM
通过jconsole.exe来查看,jconsole.exe所在目录为/%JAVA_HOME%/bin/jconsole.exe 需要配置: -Dcom.sun.management.jmxrem ...
- sql 常用基础查询
*查询第一条记录:select top 1 * from tablename *随机查询N条记录:select top N * from table order by newid( ...
- Asp.net Core IIS上安装部署及502.5错误解决
总结: 安装Microsoft Visual C++ 2015 Redistributable(https://www.microsoft.com/en-us/download/details.asp ...
- util.Date转化成sql.date格式
https://www.cnblogs.com/zhaotiancheng/p/6413067.html