设为首页 收藏本站
查看: 3290|回复: 0

[经验分享] ef 下解决 C# 中函数 Convet 到sql 不识别问题

[复制链接]

尚未签到

发表于 2018-10-20 08:56:40 | 显示全部楼层 |阅读模式


SqlFunctions.StringConvert,。。。。。Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"及其他问题 转换 问题解决)
  C#(50) DSC0000.jpg
  版权声明:本文为博主原创文章,未经博主允许不得转载。
  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        select new ListItem  
        {
  
            Value = SqlFunctions.StringConvert((double)c.ContactId),
  
            Text = c.Name
  
        };
  share|improve this answer
  edited Jul 22 '10 at 21:25
  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?) 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上没有)



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-623885-1-1.html 上篇帖子: PL/SQL developer的安装使用与配置(整理) 下篇帖子: Rancher Server部署方式及Rancher HA环境部署
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表