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

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

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

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

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

    DIRequest reqeust = new DIRequest();            

    reqeust.FilterMode = "1,2,3,4,5,6,7,8,9,0";

    List<int> result = new List<string>(reqeust.FilterMode.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); }
  • 相关阅读:
    AutoMapper 模型转换
    HttpClient Post请求
    C#根据中文description获取enum枚举值
    SQLserver链接字符串MySql链接字符串区别
    EF+Linq分组 多条件
    netcore导入 取Excel中的数据做操作
    net core文件流导出文件
    V-IF 使用包含判断条件
    Vue 使用mounted 或created自动调用函数,遇到的第一个问题 this 指针指向
    VUE 属性绑定
  • 原文地址:https://www.cnblogs.com/EasyLive2006/p/2237320.html
Copyright © 2011-2022 走看看