• 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 重新匹配 查询一次。 算是个 解决问题的笨方法。

  • 相关阅读:
    Mysql存储过程和函数
    python反编译chm文件并生成pdf文件
    python转换html到pdf文件
    python获取系统开机时间
    OpenSL ES: 利用OpenSL ES实现录音功能
    android: 根据文件uri 获取文件名
    Java: InputStream转化为byte数组
    Linux: 查看二进制文件
    Vim: 回到上次编辑的位置
    LayoutInflate: Avoid passing null as the view root
  • 原文地址:https://www.cnblogs.com/90nice/p/3695562.html
Copyright © 2020-2023  润新知