根据用户输入的起始日期,查询以起始日期开始的前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 解决办法的更多相关文章

  1. 关于.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 ...

  2. 报错:System.NotSupportedException: LINQ to Entities does not recognize the method

    报错:System.NotSupportedException: LINQ to Entities does not recognize the method ...... get_Item(Int3 ...

  3. LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法

    一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 关于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实现序列化接口即可

  8. LINQ to Entities不支持Convert.ToDateTime方法解決一例

    錯誤提示: LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' met ...

  9. LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。

    var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new { MatNR = o.MatNR, ...

随机推荐

  1. 【笔记】metasploit渗透测试魔鬼训练营-信息搜集

    exploit 漏洞利用代码 编码器模块:免杀.控制 help [cmd] msfcli适合对网络中大量系统统一测试. 打开数据包路由转发功能:/etc/sysctl.conf /etc/rc.loc ...

  2. 使用zookeeper自带的zkCli.sh客户端工具实现对zk的CURD常见操作详解

    一.zookeeper自带的 zkCli.sh 客户端工具 1. 应急和测试使用到的一个工具. 还有C# dirver java dirver (驱动)   二.driver的使用方式有两种 zkCl ...

  3. [LeetCode 题解]: Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  4. Linux带有时间控制的多进程bash脚本

    目标 以可控制的多进程执行,达到最大执行时长后停止脚本. 思路 1.产生fifo管道,并预填充n个值(与并发数相等) 2.记录脚本本身PID并启动计时器进程(计时终止后杀脚本本身PID) 3.并发执行 ...

  5. Django博客项目思路整理

    首先明确一点,我目前学习Django是为了做一个博客,那么以博客为目标进行实践的话,按照Django的MTV模型的顺序来思考的话,要考虑如下几个事情: (Models) 1.在博客里的各种数据模型: ...

  6. 一起学习《C#高级编程》2--比较对象的相等性

    今后争取每两天能更新一次.平日的诱惑太多,双休只顾玩了,进度有点慢. 接上一讲的,类型的安全性,留下了点小尾巴——比较对象的相等性. C#有四种比较相等的方式:除了“==”运算符外,System.Ob ...

  7. ASP Session的功能的缺陷(进程外的Session)

    目前ASP的开发人员都正在使用Session这一强大的功能,但是在他们使用的过程中却发现了ASP Session有以下缺陷: 进程依赖性:ASP Session状态存于IIS的进程中,也就是ineti ...

  8. ASP.NET WebApi总结之自定义权限验证

    在.NET中有两个AuthorizeAttribute类, 一个定义在System.Web.Http命名空间下 #region 程序集 System.Web.Http, Version=5.2.3.0 ...

  9. linux进程管理(二)

    接上[linux进程管理(一)] 终止进程的工具 kill .killall.pkill 终止一个进程或终止一个正在运行的程序,一般是通过 kill .killall.pkill.xkill 等进行. ...

  10. js加密解密 base64

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...