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

  • 相关阅读:
    Edge 修改使用的默认搜索引擎
    VSCode 插件之
    Visual Studio Code 插件之
    一个随笔
    COGS 2479 偏序 题解
    [NOIP2015] 子串substring 题解
    [CQOI2011]动态逆序对
    树套树三题 题解
    一个随笔
    HEOI 2016 游记
  • 原文地址:https://www.cnblogs.com/90nice/p/3695562.html
Copyright © 2011-2022 走看看