I am trying to create a simple LINQ query like this:
var query = from o in entities.Orders
select new Order
{
Firstname = o.FirstName,
Lastname = GetName()
};
Here I want FirstName to be obtained from database, and I want to supply the Lastname from another source. When I run this I get an exception: “LINQ to Entities does not recognize the method ‘System.String GetName()’ method, and this method cannot be translated into a store expression.”
How can I add my own methods to a LINQ query?
You cannot, you have to solve the problem you’re having some other way, usually by splitting into two projections, one that might get translated to SQL and run on the database server, one that runs on the client.
Note: the result is not an
IQueryable, and any filtering or sorting that you might add will run on the client too.