• LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法


    一、案例1,及解决方案:

    LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式。”

    原因是LINQ to Entities 不支持ToString()函数。

    可用下述方法进行转换解决:

    string str= "1,2,3,4,5,6,7,8,9,0";

    List<int> result = new List<string>(str.Split(',')).ConvertAll(i => int.Parse(i));

    return dal.T_Common_Dy.Where(m => result.Any(a => a == m.ParentItemID.Value)).ToList();

    二、案例2,及解决方案:

    //获取市级地区public JsonResult GetCity(string id) {     var city = from c in db.AreaDivide wherec.ParentID ==int.Parse(id) select new { text =c.AreaName, value = c.ID };     return Json(city.ToList(), JsonRequestBehavior.AllowGet); }

    以上代码也会出现如下错误:

    LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式。”

    解决方案一:

    //获取市级地区public JsonResult GetCity(string id) {     int a; int.TryParse(id, out a);     var city = from c in db.AreaDivide wherec.ParentID== a select new { text = c.AreaName, value = c.ID };     return Json(city.ToList(), JsonRequestBehavior.AllowGet); }

    解决方案二:

    using System.Data.Objects.SqlClient;  //在 System.Data.Entity.dll 中
    //获取市级地区public JsonResult GetCity(string id) {     var city = from c in db.AreaDivide whereSqlFunctions.StringConvert((double)c.ParentID)== id select new { text = c.AreaName, value = c.ID };     return Json(city.ToList(), JsonRequestBehavior.AllowGet); }
  • 相关阅读:
    GUI 之 JDialog弹窗
    GUI Swing 之 JFrame窗体
    GUI 键盘监听事件
    GUI 窗口监听事件
    GUI 鼠标监听事件,模拟画图工具
    shell编程
    Ubuntu20.04 Linux初识
    rlwrap的使用
    5个相见恨晚的Linux命令,每一个都非常实用
    Bash初识与常用命令
  • 原文地址:https://www.cnblogs.com/dfyg-xiaoxiao/p/7213752.html
Copyright © 2020-2023  润新知