0

I have an extension method on an Enum called GetName which returns a string. I'm using it in linq to entity framework to select rows with a specific product name. However, when the code is getting executed it's throwing a NotSupportedException

LINQ to Entities does not recognize the method System.String GetName(Tool.ViewModels.Product) method, and this method cannot be translated into a store expression.

Here is the code I am executing:

try
{
    //Linq to Entity Framework
    var contextRow = Contexts.Data.Source.SingleOrDefault(p => p.Product == Product.ProductOne.GetName());
}
catch (Exception e)
{
    throw e;
}

Does Linq not recognize extension methods in its evaluations? Or is there something more going on?

Bukk94
  • 409
  • 1
  • 6
  • 19
Martin
  • 144
  • 10
  • this kind of problem is very well-known when starting to work with Entity Framework :) – King King Nov 20 '13 at 23:01
  • It's easy enough to get around, I'm just curious why it's happening. – Martin Nov 20 '13 at 23:04
  • 3
    "this method cannot be translated into a store expression", which means: there is no way to translate this into SQL. – Gert Arnold Nov 20 '13 at 23:05
  • 1
    the reason relates to how EF works, all the query will be translated to the underlying provider language which may be SQL Server. Because translation of many kinds of expression is not supported fully, in such cases, the exception will be thrown. – King King Nov 20 '13 at 23:07
  • @KingKing, If it was using SQL as a provider language I would understand, but I'm fairly certain its not. The data is from a view in sql server, but I enumerate it when the application starts [there is only a few rows]. – Martin Nov 20 '13 at 23:11
  • @AdmiralNelson so what kind of connection connecting your client with the data source? Unless you pull your data right from somewhere at your client side. – King King Nov 20 '13 at 23:13

2 Answers2

1

Under the hood the Entity Framework is mapping the code you type into SQL queries. When it sees the call to GetName it sees a call into a user defined function. It has no power to translate, what essentially amounts to raw IL, into a well formed SQL query. This is why you are getting that exception

JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • The problem here is, I'm caching my data. Example:http://stackoverflow.com/questions/6421127/entity-framework-4-and-caching-of-query-results So It shouldn't be going to sql, should it? – Martin Nov 20 '13 at 23:13
  • I just realized the code that was doing the caching was incorrect. Which made linq go to sql, which was causing my problem. Thanks! – Martin Nov 20 '13 at 23:20
0

when using LINQ, you have to define a context and within that context perform operations on the database, for example the code you want to run, you can be this way

using (ModelLinq _context = new ModelLinq()){

var rows= _context.anyTable.Where(p=> p.anyColumn == value);

}

And Entity Framework is a mapping of your data base for you to execute SQL queries with lenguale c #

regards!