I am using following code and trying to use a named method within Linq-To-Entities
Customer FindCustomerByLastName(string lastname)
{
DataContext MyContext = new DataContext();
return MyContext.Customers.Where(c => GetByCustomerByLastName(c,lastname) == true).FirstOrDefault();
}
bool GetByCustomerByLastName(Customer Cust, string LastName)
{
if (Cust.LastName == LastName)
return true;
else
return false;
}
But this give me following error at run time.
LINQ to Entities does not recognize the method ‘Boolean
GetByCustomerByLastName(Customer, System.String)’ method, and this
method cannot be translated into a store expression.
If I change my method to use a lambda as follows, everything works fine.
Customer FindCustomerByLastName(string lastname)
{
DataContext MyContext = new DataContext();
return MyContext.Customers.Where(c => c.LastName == lastname).FirstOrDefault();
}
Is it possible to use named method in Linq-To-Entities in the context I am trying to do?
The code inside the Linq expression has to be translatable to the environment of what’s actually being executed. This is what this part of the error message means.
The “Entities” part of Linq-to-Entities doesn’t know anything about your method. I’ve had the same problem with Linq-to-SQL where you have to use code that SQL understands.