LINQ to Entities 不识别方法"System.String ToString()"
2011-06-21 20:48
591 查看
一、案例1,及解决方案:
在做批量删除时,需把一串id值所对应的数据删除,调试出现问题:
Linq语句中如果使用ToString()进行类型转换,编译时不会报错,但执行时会出现如下错误:
“LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式。”
原因是Linq不支持ToString()函数。
可用下述方法进行转换解决:
string source = "1,2,3,4,5";
List<int> result = new List<string>(source.Split(',')).ConvertAll(i => int.Parse(i));
注: 需引用下列命名空间
using System.Collections.Generic;
using System.Linq;
二、案例2,及解决方案:
//获取市级地区
public JsonResult GetCity(string id)
{
var city = from c in db.AreaDivide where c.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 where c.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 where SqlFunctions.StringConvert((double)c.ParentID) == id select new { text = c.AreaName, value = c.ID };
return Json(city.ToList(), JsonRequestBehavior.AllowGet);
}
相关资料:
http://archive.cnblogs.com/a/1878714/
http://stackoverflow.com/questions/1228318/linq-int-to-string
http://stackoverflow.com/questions/1066760/problem-with-converting-int-to-string-in-linq-to-entities
在做批量删除时,需把一串id值所对应的数据删除,调试出现问题:
Linq语句中如果使用ToString()进行类型转换,编译时不会报错,但执行时会出现如下错误:
“LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式。”
原因是Linq不支持ToString()函数。
可用下述方法进行转换解决:
string source = "1,2,3,4,5";
List<int> result = new List<string>(source.Split(',')).ConvertAll(i => int.Parse(i));
注: 需引用下列命名空间
using System.Collections.Generic;
using System.Linq;
二、案例2,及解决方案:
//获取市级地区
public JsonResult GetCity(string id)
{
var city = from c in db.AreaDivide where c.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 where c.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 where SqlFunctions.StringConvert((double)c.ParentID) == id select new { text = c.AreaName, value = c.ID };
return Json(city.ToList(), JsonRequestBehavior.AllowGet);
}
相关资料:
http://archive.cnblogs.com/a/1878714/
http://stackoverflow.com/questions/1228318/linq-int-to-string
http://stackoverflow.com/questions/1066760/problem-with-converting-int-to-string-in-linq-to-entities
相关文章推荐
- LINQ to Entities 不识别方法"System.String ToString()"
- “LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式。”
- Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)
- LINQ to Entities 不识别方法"System.String ToString()"
- Linq中int转String(解决LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式.)
- LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法
- LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法
- Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)
- LINQ to Entities 不识别方法“Boolean IsNullOrWhiteSpace(System.String)”
- 【福分系统】 LINQ to Entities 不识别方法“System.DateTime ToDateTime(System.String)”,因此该方法无法转换为存储表达式。
- LINQ to Entities 不识别方法“System.String ToString(System.String)”因此该方法无法转换为存储表达式
- LinQ to entities 不能识别方法“system.string.ToString(system.String)”.因此该方法无法转换为存储表达式
- LINQ to Entities 不识别方法“Int32 ToInt32(System.String) 或 “Int32 Parse(System.String)”
- LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
- LINQ to Entities 不识别方法system.string
- LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
- LINQ to Entities 不识别方法“Boolean IsNullOrWhiteSpace(System.String)”
- LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
- 报错: LINQ to Entities 不识别方法“Int32 Parse(System.String)
- LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法