I have a problem getting result from a database by specific column value using LINQ.
This is my table Customer(Id, Name, Surname, ExternalID). ExternalID is a varchar column with specific string pattern, for instance 01_johnDoe.
Let’s say that I want to get customer johnDoe and my mehod gets string johnDoe.
I’m able to do that by this way :
public<Customer> GetMeACustomer(string customerExternalID)
{
using(var context=new MyContext())
{
var customerObject=(from c in contex.Customer where c.ExternalID.Contains(customerExternalID)).Single()
}
}
This code is not good enough simply because query sent to Database is using operator LIKE. This is slow (I have huge number of customer).
I couldn’t implement inside linq something like this :
var example = ExternalIDFromDatabase.Substring(ExternalIDFromDatabase.IndexOf("_") + 1, customerExternalID.Count());
by this code I could simply say
var customerObject=(from c in contex.Customer where c.ExternalID.Substring(c.ExternalID.IndexOf("_") + 1, customerExternalID.Count())==customerExternalID).Single()
But it’s not working.
Any Ideas ?
Than you
It should work when you replace
customerExternalID.Count()withcustomerExternalID.Lengthor when you create local variable (let saycustomerExternalIDCount) and assign count to it. Probably provider tries to translate thisCountcall into SQL.BTW you probably want to use
Substringmethod without specifinglength. If there was for example01_johnRobinand02_johnRobinsin database, you would get 2 results forjohnRobincustomerExternalID.