LINQ to Entities works by translating LINQ queries to SQL queries, then executing the resulting query on the database and converting the result back to application entities. Because of this crossover of two languages, it will only work when SQL-compatble method calls are made. When an incompatible call is made, it’ll throw an exception along the lines of:

LINQ to Entities does not recognize the method 'xxx' method, and this method cannot be translated into a store expression.

For example the following code will throw this exception:

var result = db.Users.Select(p => new { BirthYear = Convert.ToInt16(p.Year) }).ToList();

Because SQL doesn’t have the .NET based System.Convert class, it can’t translate this call to SQL and hence raises the exception. The way around this is to retrieve the data first, then do any .NET transforms or function calls after:

var result = db.Users.ToList().Select(p => new { BirthYear = Convert.ToInt16(p.Year) }).ToList();

The only difference is that ToList() (or any other method that will execute an IQueryable) is called first that brings the Users data into .NET memory, with the subsequent Convert option done aftewards.

“DataContext accessed after Dispose” error:解决方法,在最后加上.ToList()

关于.ToList(): LINQ to Entities does not recognize the method ‘xxx’ method, and this method cannot be translated into a store expression.的更多相关文章

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

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

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

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

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

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

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

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

  7. LINQ to Entities 不支持 LINQ 表达式节点类型“ArrayIndex”

    我就不屁话,能一张图就解决的就不说话了 2015-03-28 14:53:24,440 [10] ERROR log - System.NotSupportedException: LINQ to E ...

  8. Linq to entities 学习笔记

    Linq to  entities ---提供语言集成查询支持用于在概念模型中定义的实体类型. 首先可以根据http://msdn.microsoft.com/en-us/data/jj206878该 ...

  9. LINQ to Entities不识别方法***,因此该方法无法转换为存储表达式

    我的程序里有这么一段代码: order.OrderExpressInfo = (from oei in orderExpressRepository.Entities where oei.OrderI ...

随机推荐

  1. VBA 打印设置相关属性及方法

    打印设置说明,以下均为默认值. With ActiveSheet.PageSetup .PrintTitleRows = "" '工作表打印标题:顶端标题行(R) .PrintTi ...

  2. new Date() 倒计时

    js中单独调用new Date() 显示的结果是:Fri May 20 2015 20:00:00 GMT+0800这种格式的时间 JS获取当前时间戳的方法 JavaScript 获取当前时间戳: 第 ...

  3. Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)

    3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...

  4. Linux的前世今生

    Linux的起源 说到Linux[/ˈlɪnəks/],想必大家也会自然而然地想到他的创始人——被称为“Linux之父”的林纳斯·托瓦兹(Linus Torvalds).其实,在Linux出现之前,还 ...

  5. (转)Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage

    ,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata e ...

  6. Python没有执行__init__

    疑惑 提出问题 前天同事问我一个问题,为什么这个脚本中的没有调用A 的__init__.脚本如下: class A(object): def __init__(self, *args, **kwarg ...

  7. a标签使用

    1.发起邮件 注意:如果mailto后面同时有多个参数的话,第一个参数必须以“?”开头,后面的参数每一个都以“&”分隔. <a href="mailto:281345774@q ...

  8. MVC中Area的使用

    1.Area是什么? MVC 2 中引进了区域的概念,它允许将模型,视图和控制器分成单独的功能节点,换句话说,可以在大型复杂的网站中建立几个区域(模块),每一个区域都有Model,View,Contr ...

  9. python入门 第二天笔记

    程序主文件标志if __name__=="__main__": 在程序执行python 1.py 时候 程序1.py __name__ 为 main调用其他文件是,__name__ ...

  10. HTML 页面加载动画效果

    浏览器:Chrome, IE <!doctype html> <html> <head> <title>CSS transform: CSS only ...