.net 实体框架4.0使用子字符串使用linq进行查询
我在使用LINQ通过特定列值从数据库中获取结果时遇到问题 这是我的表客户(Id、姓名、姓氏、外部Id)。ExternalID是具有特定字符串模式的varchar列,例如01_johnDoe 假设我想得到客户约翰多,我的方法得到字符串约翰多 我可以这样做:.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
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()