Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 实体框架4.0使用子字符串使用linq进行查询_.net_Linq_Entity Framework 4 - Fatal编程技术网

.net 实体框架4.0使用子字符串使用linq进行查询

.net 实体框架4.0使用子字符串使用linq进行查询,.net,linq,entity-framework-4,.net,Linq,Entity Framework 4,我在使用LINQ通过特定列值从数据库中获取结果时遇到问题 这是我的表客户(Id、姓名、姓氏、外部Id)。ExternalID是具有特定字符串模式的varchar列,例如01_johnDoe 假设我想得到客户约翰多,我的方法得到字符串约翰多 我可以这样做: public<Customer> GetMeACustomer(string customerExternalID) { using(var context=new MyContext()) { var customerObjec

我在使用LINQ通过特定列值从数据库中获取结果时遇到问题

这是我的表客户(Id、姓名、姓氏、外部Id)。ExternalID是具有特定字符串模式的varchar列,例如01_johnDoe

假设我想得到客户约翰多,我的方法得到字符串约翰多

我可以这样做:

public<Customer> GetMeACustomer(string customerExternalID)
{
using(var context=new MyContext())
{

var customerObject=(from c in contex.Customer where c.ExternalID.Contains(customerExternalID)).Single()   

}

}
根据这段代码,我可以简单地说

var customerObject=(from c in contex.Customer where c.ExternalID.Substring(c.ExternalID.IndexOf("_") + 1, customerExternalID.Count())==customerExternalID).Single()
但它不起作用。 有什么想法吗?
当您用
customerExternalID.Length
替换
customerExternalID.Count()
时,或者当您创建局部变量(比如
customerExternalID Count
)并为其分配计数时,它应该可以工作。可能提供者试图将此
Count
调用转换为SQL

顺便说一句,您可能希望使用
子字符串
方法而不指定
长度
。例如,如果数据库中有
01_johnRobin
02_johnRobins
,您将得到
johnRobin
customerExternalID

的两个结果,请检查此项
var customerObject=(from c in contex.Customer where c.ExternalID.Substring(c.ExternalID.IndexOf("_") + 1, customerExternalID.Count())==customerExternalID).Single()