首页 新闻 赞助 找找看

linq to Entity 的悲哀

0
[待解决问题]

SQl 代码

select * from Customer
where
substring(CustomerTelp, 0,charindex('/',CustomerTelp))='13906031273' or
substring(CustomerTelp, charindex('/',CustomerTelp)+1,len(CustomerTelp))='13906031273'

 

转换长linq to Entity 

var p = from q in pedm.Customer
           where q.CustomerTelp.Substring(0,q.CustomerTelp.IndexOf('/')) == strTelp
           select q;

回报错:

LINQ to Entities 不识别方法“Int32 IndexOf(Char)”,因此该方法无法转换为存储表达式。

 

补充下 不能是用like 要分割出来精确查找  CustomerTelp为varchar()数据类型 由于之前数据库设计有误 现在想用电话作为唯一键 

求高手啊

yellowshorts的主页 yellowshorts | 初学一级 | 园豆:162
提问于:2012-11-22 11:35
< >
分享
所有回答(5)
0

 q.CustomerTelp.Contans(strTelp)

Launcher | 园豆:45045 (高人七级) | 2012-11-22 11:49
0

也可以用 lint to entity专门 调用数据库函数的方法

System.Data.Objects.EntityFunctions  和System.Data.Objects.SqlClient.SqlFunctions
q.CustomerTelp.Substring(0,System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/",q.CustomerTelp).Value) == strTelp

 


Qlin | 园豆:2403 (老鸟四级) | 2012-11-22 13:06

试了 

还是没达到效果  返回var p 返回是空的

支持(0) 反对(0) yellowshorts | 园豆:162 (初学一级) | 2012-11-22 14:01

@yellowshorts: 

查看 生成的 sql语句,看看问题所在 或者 去 查询分析器 执行 看看

支持(0) 反对(0) Qlin | 园豆:2403 (老鸟四级) | 2012-11-22 14:04

@Qlin: where q.CustomerTelp.Substring(0, System.Data.Objects.SqlClient.SqlFunctions.

CharIndex("/", q.CustomerTelp).Value) == strTelp

这样生成的是 '15555558/' 

我在上面直接 在上面减1 会报错

q.CustomerTelp.Substring(0, System.Data.Objects.SqlClient.SqlFunctions.

CharIndex("/", q.CustomerTelp).Value-1) == strTelp 
支持(0) 反对(0) yellowshorts | 园豆:162 (初学一级) | 2012-11-22 14:21

@yellowshorts: 

报什么错, 难道返回null了, 加个 默认值试试 q.CustomerTelp.Substring(0,System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/", q.CustomerTelp)??0-1)

支持(0) 反对(0) Qlin | 园豆:2403 (老鸟四级) | 2012-11-22 14:42

@yellowshorts: 

var p = from q in pedm.Customer
where q.Id==2
select new
{
aaaa = System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/", q.CustomerTelp).Value - 1,
bbbb = q.CustomerTelp.Substring(0, System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/", q.CustomerTelp).Value - 1)
};

int aaaa = p.FirstOrDefault().aaaa;
strTelp = p.FirstOrDefault().bbbb;

如果写成这样 没有出错 

但是 写在里面

var p = from q in pedm.Customer
where q.CustomerTelp.Substring(0,System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/", q.CustomerTelp).Value - 1)

== strTelp
select new
{
aaaa = System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/", q.CustomerTelp).Value - 1,
bbbb = q.CustomerTelp.Substring(0, System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/", q.CustomerTelp).Value - 1)
};
int aaaa = p.FirstOrDefault().aaaa;
strTelp = p.FirstOrDefault().bbbb;

支持(0) 反对(0) yellowshorts | 园豆:162 (初学一级) | 2012-11-22 15:10
0

startwith来替代吧

chenping2008 | 园豆:9836 (大侠五级) | 2012-11-22 13:10
0

这个题目起的有点...

CustomerTelp 在数据库的类型是整型吗?

jerry-Tom | 园豆:4077 (老鸟四级) | 2012-11-22 13:49

不是整形 在数据库是varchar 

支持(0) 反对(0) yellowshorts | 园豆:162 (初学一级) | 2012-11-22 14:02

@yellowshorts: IndexOf("/")

改成双引号试试

支持(0) 反对(0) jerry-Tom | 园豆:4077 (老鸟四级) | 2012-11-22 15:04
0

var p = from q in pedm.Customer
           where q.CustomerTelp.where(c=>

c.CustomerTelp!= null && c.CustomerTelp.Trim() != "" && c.CustomerTelp.Contains("/"))


           select q;

var query=p.ToList().

Where(c => c.CustomerTelp.Substring(0, c.Value.IndexOf('/'))=="strTelp")

;

RyanCheng | 园豆:474 (菜鸟二级) | 2012-11-27 12:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册