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

本文针对 LINQtoEntities 不支持 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); }
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值