您的位置:首页 > 其它

LINQ to Entities 不识别方法“System int string 转换的问题

2014-04-28 02:03 477 查看
这个问题困扰了挺久,网上找了挺多方法 都太好使。

分几种情况。

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 重新匹配 查询一次。 算是个 解决问题的笨方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐