zoukankan      html  css  js  c++  java
  • LINQ to Entities 不识别方法“System int string 转换的问题

    这个问题困扰了挺久,网上找了挺多方法 都太好使。

    分几种情况。

    1.如果查询结果 转换,那比较容易。

    var q = from c in db.Customers

         where c.Country == "UK" || c.Country == "USA"

         select new

         {

           Phone = c.Phone,

           InternationalPhone =

           PhoneNumberConverter(c.Country, c.Phone)

         };

    public string PhoneNumberConverter(stringCountry, string Phone){此处省略}

    可以自定义 转换格式 随意怎么写。

    2.如果是查询条件中转换,比如 A表的 id (int型) 与B 表的 id(varchar) 进行匹配的时候

    需要用到SqlFunctions.StringConvert((decimal)a.id, 10, 0) == b.id

    网上最终的解决方法就是这个。 但是如果 B表的id 是 “  1”,那就没办法匹配了

    我最终的解决办法是 这样的。

    var a = (from a in _db.a select a).tolist();

    var b = (from a in _db.a select a).tolist();

    var c = (from aa in a

        from bb in b

        where a.id.tostring() == b.id select new{a.id}).tolist();

    先查询出来 A,B的数据 用 linq to list 进行匹配,不在 linq to entity 中进行。因为 linq to entity 中不支持很多函数 .tostring , int.parse() 不支持

    所以转到 list 重新匹配 查询一次。 算是个 解决问题的笨方法。

  • 相关阅读:
    设计模式第四篇-工厂模式
    设计模式第三篇-装饰者模式
    设计模式第二篇-观察者模式
    设计模式第一篇-策略模式
    一元多项式的加/减法运算
    圆桌问题
    求有序序列的交集(链表)
    悲剧文本
    求序列的交集(链表)
    集合的操作
  • 原文地址:https://www.cnblogs.com/90nice/p/3695562.html
Copyright © 2011-2022 走看看