LINQ to Entities 不识别方法"System.String ToString()"

简介: 一、案例1,及解决方案: 在做批量删除时,需把一串id值所对应的数据删除,调试出现问题: Linq语句中如果使用ToString()进行类型转换,编译时不会报错,但执行时会出现如下错误: “LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式。

一、案例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

学习交流群:364976091
相关文章
|
2月前
|
Java
Java String split()方法详细教程
Java String split()方法详细教程
27 0
|
5月前
|
Go
Go string bytes、strings、strconv和unicode包相关方法
Go string bytes、strings、strconv和unicode包相关方法
30 0
|
7月前
|
Java
每日一道面试题之String常用的方法有哪些?
每日一道面试题之String常用的方法有哪些?
|
4月前
|
Java
String类中的一些常用方法(JAVA)
字符串比较方法: boolean equals(Object anObject):  int compareTo(String s): int compareToIgnoreCase(String str) 字符串查找方法: char charAt(int index): int indexOf(int ch):  int indexOf(int ch, int fromIndex): int indexOf(String str): int indexOf(String str, int fromIndex): int lastIndexOf(int ch): int
61 0
|
2月前
|
Java 索引
Java中String方法学习总结_kaic
Java中String方法学习总结_kaic
|
2天前
int 和 String 互相转换的多种方法
int 和 String 互相转换的多种方法
|
18天前
|
C++
【C++】std::string 转换成非const类型 char* 的三种方法记录
【C++】std::string 转换成非const类型 char* 的三种方法记录
7 0
|
2月前
|
Java 索引
【Java】String类常用方法总结
【Java】String类常用方法总结
20 0
|
2月前
|
机器学习/深度学习 Java 索引
39、一篇文章弄懂 Java 正则表达式中的量词、贪婪、勉强、独占和 String 的 matches 方法的底层【个人感觉非常值得学习】
39、一篇文章弄懂 Java 正则表达式中的量词、贪婪、勉强、独占和 String 的 matches 方法的底层【个人感觉非常值得学习】
32 0
|
2月前
|
存储 安全 Java
36、Java 中的 String、StringBuilder、StringBuffer、字符串常量池和 intern 方法
36、Java 中的 String、StringBuilder、StringBuffer、字符串常量池和 intern 方法
35 0