The code below produces the error:
LINQ to Entities does not recognize the method
System.String GenerateSubscriptionButton(Int32)method, and this method cannot be translated into a store expression.
How do I create correct custom methods in LINQ to Entities?
var model = _serviceRepository.GetProducts().Select(p => new ProductModel
{
Id = p.Id,
Name = p.Name,
Credits = p.Credits,
Months = p.Months,
Price = p.Price,
PayPalButton = GenerateSubscriptionButton(p.Id)
});
private string GenerateSubscriptionButton(int id)
{
return new PaymentProcessor.PayPalProcessor().CreateSubscriptionButton(id);
}
You can’t do that. How should the provider translate your method to SQL?
Remember: LINQ to Entities doesn’t actually execute the C# code of your queries. Instead, it interpretes the expressions and translates them to SQL.
In your conrete case, the solution would probably look something like this:
The call to
ToListexecutes the query so far against the database and returns the result. From that point forward, the query actually is a LINQ to objects query where the code is not interpreted but executed.