1

Using our previous ORM, OpenAccess, we were able to include helper methods in the select statements of our queries. For example, to combine SQL data with cached application data.

After switching to Entity Framework 6.x we're getting errors like this:

LINQ-to-Entities does not recognize the method 'System.String GetProductTranslation'

The ling query looks like this:

var products = (from p in db.Products
                join cp in db.CustomerPrices on p.ProductId equals cp.ProductId

                where p.LockedSince.Equals(null)
                && ... etc etc etc

                select new
                {
                    ProductId = p.ProductId,
                    Name = TranslationHelper.GetProductTranslation(p.ProductId, ProductTranslationField.Name, p.Name),
                    Description2 = TranslationHelper.GetProductTranslation(p.ProductId, ProductTranslationField.Description2, p.Description2),
                    Description3 = TranslationHelper.GetProductTranslation(p.ProductId, ProductTranslationField.Description3, p.Description3),
                    Description4 = TranslationHelper.GetProductTranslation(p.ProductId, ProductTranslationField.Description4, p.Description4),
                    ... etc etc etc
                });

In this case the GetProductTranslation method grabs translation data from the application cache to prevent using endless amounts of joins and stress on the database.

What would be the best way to replicate this using Entity Framework 6.x?

1
  • 1
    Why just no to split out by sql query that would return nesessary data of product and another methods to retrive what you whant from cache? Commented Jan 24, 2014 at 8:21

1 Answer 1

1

You can't use custom methods with query syntax.You can see supported methods here

Instead you should use Extension methods like this:

db.Products.Join(db.CustomerPrices, 
                 p => p.ProductId,  
                 c => c.ProductId, 
                (p,c) => new { Product = p, cust = c })
            .Where(p => p.Product.LockedSince.Equals(null))
            .Select(p => new {
                       ProductId = p.Product.ProducId,
                       Name = TranslationHelper.GetProductTranslation(p.Product.ProductId, ProductTranslationField.Name, p.Product.Name),
                       ...
                       });
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure what to make of your code example. All I can see is the use of the lambda syntax. Switching to the other syntax did not make a difference in the exception that is thrown by EF. For now I've seperated the two, like suggested by Vladimir Shmidt above. But I would still love to see the two integrated like we had before.
@Erik, your query converting this lambda syntax behind the scenes, but there is a restriction in query synxtax.you can't use unsupported methods,when you try to use unsupported method in query syntax,it is not recognized because there is no mapping for this method.Using directly lambda syntax you can use whatever you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.