1、在工作中碰到这样一个问题:

使用linq时,需要查询两个表,在这两张表中关联字段分别是int,和varchar()也就是string,在linq中对这两个字段进行关联,

如果强制类型转换两个不同类型的字段,就会报响应的扩展方法无法自动推断参数类型的问题(比如:我用的是groupjoin扩展方法),

如果进行了常规的类型转换,比如将int字段对应的转换为string(ToString方法),这时编译的时候不会有问题了。

但是在运行的时候会报如下错误:

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

2、解决方法:

使用System.Data.Objects.SqlClient.SqlFunctions.StringConvert()对相应的字段进行转换,结果可以了。

比如:

我有问题的Linq是下面这个:

var borrowLeftOutJoinLog =
                query.GroupJoin(
                    groupRtnToolByID,
                    c => c.ID.ToString(),
                    d => d.BizID, (g, f) => new { Item = g, Detail = f })
                    .SelectMany(detail => detail.Detail.DefaultIfEmpty(), (a, b) => new { a.Item, RtnSum = (decimal?)b.RtnSum })
                    .Where(a => (a.RtnSum ?? 0) < a.Item.TBor_Qty)
                    .Select(a => a.Item);

注意:上面的ID和BizID是有关联的,但是在两个表中分别被设计成了int,string,所以这里对ID进行了ToString的转换,结果出现了上面提示的错误。

运用解决方法后的语句如下:

var borrowLeftOutJoinLog =
                query.GroupJoin(
                    groupRtnToolByID,
                    c => System.Data.Objects.SqlClient.SqlFunctions.StringConvert((double)c.ID),
                    d => d.BizID, (g, f) => new { Item = g, Detail = f })
                    .SelectMany(detail => detail.Detail.DefaultIfEmpty(), (a, b) => new { a.Item, RtnSum = (decimal?)b.RtnSum })
                    .Where(a => (a.RtnSum ?? 0) < a.Item.TBor_Qty)
                    .Select(a => a.Item);

3、后感:

遇到这个问题后,自己尝试了解决,可是都行不通,于是转而求助网络(感谢这个时代吧!!!),

也看到了几篇关于这个的解决方法,其中就包括ToString方法,而且还言之凿凿,再不就是说一些框架,底层问题,

难道我为了这个芝麻大点的事,也要去自己重写,去实现很多内容吗??

如果是,只能说我选错了技术,它还不成熟。

可是,结果不是这样的,Linq出来已经很久了,它包含的内容也是很多,不应该是这样的,所以我继续找

在这里我找到了:

http://stackoverflow.com/questions/1066760/problem-with-converting-int-to-string-in-linq-to-entities

这句介绍了问题的原因:StingyJack, the problem is with the ELINQ (linq 2 entities), because it translates your code to SQL, and when it comes to an inner ToString request, it doesn't know how to translate 'ToString' to SQL. Unlike with linq 2 objects, when there is no translation, and everything is CLR lambdas, then it's performed directly on the requested objects;

下面这个给了具体的解决方法:

http://stackoverflow.com/questions/1066760/problem-with-converting-int-to-string-in-linq-to-entities/3292773#3292773

up vote126down voteaccepted

With EF v4 you can use SqlFunctions.StringConvert. There is no overload for int so you need to cast to a double or a decimal. Your code ends up looking like this:

var items =from c in contacts
selectnewListItem{Value=SqlFunctions.StringConvert((double)c.ContactId),Text= c.Name};
 
 
answered Jul 20 '10 at 17:44
Brian Cauthon 2,96111121
 
99  
Why on earth wouldn't they include an overload for int? – Jeremy Coenen Oct 1 '10 at 16:59
1  
What does the SqlFunctions.StringConvert part of your query look like? From the error it sounds like you are passing a Nullable<double> (double?) instead of a double. Try callingGetValueOrDefault() before you pass it in. – Brian Cauthon Apr 25 '11 at 13:23
5  
@Nestor This doesn't work for SQL Compact. Found that out the hard way. – Austin Nov 8 '11 at 21:04
3  
To avoid the whitespaces before the result, you should useSqlFunctions.StringConvert((double)c.ContactId).Trim() – Kim Tranjan Mar 15 '12 at 3:47
1  
Seems not to work for SQLite using System.Data.SQLite The Methode 'System.String StringConvert(System.Nullable`1[System.Double])' in Typw 'System.Data.Objects.SqlClient.SqlFunctions' kann nicht in einen Speicherausdruck für 'LINQ to Entities' übersetzt werden. (cannot be translated into "LINQ to Entities") – OneWorld Jan 9 at 10:28

show 4 more comments

所以,我觉得回答别人问题时要慎重,要针对别人给出的条件自己去试验,不能简单凭经验,不能说空话,更不能想当然。

给出结果要直接(当然如果要是一语中的,切中要害,那当然不用直接给出,可是我没有这样的功力,起码在linq上没有)。

原文地址:http://blog.csdn.net/xiaojia_boke/article/details/8786362

Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)的更多相关文章

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

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

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

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

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

    来源:https://www.cnblogs.com/hao-1234-1234/p/9112434.html 6  Select的时候,时间无法转换成 年月日  YYMMMdd 报错:LINQ to ...

  4. LinQ to entities 不能识别方法“system.string.ToString(system.String)”.因此该方法无法转换为存储表达式

    [我也是刚研究IEnumerable和IQueryable]以下都是个人理解,仅供参考,如有错误欢迎指出~ 在EF里面,使用IQueryable和IEnumerable可以延迟加载. IQueryba ...

  5. LINQ to Entities 不识别方法“System.String ToString(“yyyy-MM-dd”)”

    将Queryable转化为IEnumerable或者直接Tolist()

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

    1.LINQ to Entities 不识别方法“System.String get_Item(Int32)”,因此该方法无法转换为存储表达式.项目中发现linq to entities 不识别? , ...

  7. linq to entity不识别方法"System.String ToString()"

    将班级id以字符串形式输入如:"1111,1112,1113".数据库里的id为int型,在数据路里找到匹配的相应班级转换成列表.在这里爆出问题:不识别方法"System ...

  8. LINQ to Entities 不识别方法“System.DateTime AddDays(Double)

    今天本想在linq里按照时间筛选一下超时的数据,一共两个字段FeedBackTime(计划反馈时间).EndTime(实际反馈时间).需求是这样的,查找数据库里所有EndTime大于FeedBackT ...

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

    LINQ to Entities 不识别方法"System.Guid Parse(System.String)",因此该方法无法转换为存储表达式. linq 中不能转换类型

随机推荐

  1. Angular2案例rebirth开源

    Angular2案例rebirth开源 在过去的几年时间里,Angular1.x显然是非常成功的.但由于最初的架构设计和Web标准的快速发展,逐渐的显现出它的滞后和不适应.这些问题包括性能瓶颈.滞后于 ...

  2. java 属性

    //非静态类 不能定义静态属性/方法/静态类, 可以定义静态常量属性. public class A{ public class B{ public static String  _str; //❌, ...

  3. 洛谷 P1331 海战

    传送门 题解:由于船是方形的,所以比较简单.但是考试的时候跪了,orz.忘了考虑类似一圈井号中间有一摊水.          可以只考虑这个点上方和左边点的情况,这样分为四种情况.一种是左边是一滩水, ...

  4. BZOJ 3065 带插入区间K小值

    http://www.lydsy.com/JudgeOnline/problem.php?id=3065 思路:替罪羊树套权值线段树. 当替罪羊树某个子树大于某个比利(比例)时就暴力重构,本题时间复杂 ...

  5. 【转】整理一下Android中的ListView

    原文网址:http://sunbofu.blog.51cto.com/6431507/1280441 Android中的listview目测是一个使用频率很高的组件,所以今天来总结一下listview ...

  6. 辗转相除法_欧几里得算法_java的实现(求最大公约数)

    辗转相除法,又被称为欧几里德(Euclidean)算法, 是求最大公约数的算法. 当然也可以求最小公倍数. 算法描述 两个数a,b的最大公约数记为GCD(a,b).a,b的最大公约数是两个数的公共素因 ...

  7. 火狐解决 OCSP 回应包含过期信息的问题_firefox吧_百度贴吧

    火狐解决 OCSP 回应包含过期信息的问题_firefox吧_百度贴吧 火狐解决 OCSP 回应包含过期信息的问题

  8. fedora19配置 SSH 免密码登陆

    a.ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa b.cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys   ...

  9. css文本超出2行就隐藏并显示省略号

    之前在网上看到过这样的代码,感觉有的时候还是挺有用的,故留个笔记. display:-webkit-box; //将对象作为弹性伸缩盒子模型显示. -webkit-box-orient:vertica ...

  10. hibernate初涉

    好久都不曾写写总结一些东西了,惰性真的是令人难以克制!虽然和许多北漂族一样,艰苦而又迷茫,但是我总能找到一些方向,一点期盼,因为你就我的目标.我会坚持下去,重拾青春的热血,既然人生如戏,那我不当猪脚. ...