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()数据类型 由于之前数据库设计有误 现在想用电话作为唯一键
求高手啊
q.CustomerTelp.Contans(strTelp)
也可以用 lint to entity专门 调用数据库函数的方法
q.CustomerTelp.Substring(0,System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/",q.CustomerTelp).Value) == strTelp
试了
还是没达到效果 返回var p 返回是空的
@yellowshorts:
查看 生成的 sql语句,看看问题所在 或者 去 查询分析器 执行 看看
@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
@yellowshorts:
报什么错, 难道返回null了, 加个 默认值试试 q.CustomerTelp.Substring(0,System.Data.Objects.SqlClient.SqlFunctions.CharIndex("/", q.CustomerTelp)??0-1)
@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;
startwith来替代吧
这个题目起的有点...
CustomerTelp 在数据库的类型是整型吗?
不是整形 在数据库是varchar
@yellowshorts: IndexOf("/")
改成双引号试试
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")
;