-1

Possible Duplicate:
LINQ to Entities does not recognize the method

I use Entity Framework 4.3

I write extension method:

public static IQueryable<TSource> Active<TSource>(this IQueryable<TSource> source) where TSource : class, IStatusable
{
    return source.Where(s => s.Status == (int)StatusEnum.Enabled);
}

This works good:

var cat=Context.Categories.Active().ToList()

But i need use this extension method in Select. Look simplified query:

return Context.Categories
 .Select(c => new { Children=c.Children.AsQueryable().Active()})
 .ToList()

(Children - collection of child categories) When query execution I get a error message:

LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[Portal.FrontOffice.Model.Category] Active[Category](System.Linq.IQueryable`1[Portal.FrontOffice.Model.Category])' method, and this method cannot be translated into a store expression.

Why does not work? How to write correctly?

Community
  • 1
  • 1
DeeRain
  • 1,344
  • 1
  • 15
  • 24
  • There are literaly [thousands](http://stackoverflow.com/search?q=LINQ+to+Entities+does+not+recognize+the+method+) of questions with this error message. – Daniel Hilgarth Sep 05 '12 at 09:31
  • I guess you do not read the question carefully. This error message occurs for various reasons. – DeeRain Sep 05 '12 at 09:33
  • If you read what you sent, you understand it yourself. – DeeRain Sep 05 '12 at 09:35
  • This error message occures for the very same reason every single time. – Daniel Hilgarth Sep 05 '12 at 09:35
  • I think you are not well versed in the subject. – DeeRain Sep 05 '12 at 09:39
  • You can think whatever you want. The solution to your problem is in any of the questions I linked via the link to the search. You can start reading *and understanding* them or you can continue thinking you are right and I am wrong. I don't care - it's your code that is not working. – Daniel Hilgarth Sep 05 '12 at 09:41
  • Yes, you can think whatever you want. But as human being capable of critical thinking you should reconsider your answer as it is obviously not related to the question. It was stated that EF actually understands the extension method I have written unless it is used within IQueryable.Select. And it is not any of the cases reviewed in the threads you have provided. – DeeRain Sep 05 '12 at 10:05

1 Answers1

2

As stated in my comments, it is the very same reason every time this error message appears:

The expression tree that is used by the EF provider to create the SQL contains a method it doesn't understand.
In your case, this is the Active extension method. It is part of the expression tree as it is used inside another expression (Select).

In your first query, your method is NOT part of the expression tree. Instead it simply changes the expression tree by adding the Where expression to it. That is a fundamental difference.

To make your second query work, use this:

return Context.Categories 
              .Select(c => new { Children=c.Children
                                           .Where(s => s.Status == 
                                                       (int)StatusEnum.Enabled) }) 
              .ToList() 
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Thanks for your answer. I apologize for my incorrect comment. – DeeRain Sep 05 '12 at 11:03
  • 1
    I know about this solution, but if I will use it i get code duplication (in Active method and in expression).So I'm looking for a way to avoid duplication. – DeeRain Sep 05 '12 at 11:07
  • The only way I found is to use the expression everywhere (instead Active). Or use some queries. – DeeRain Sep 05 '12 at 11:09
  • 2
    @DeeRain: I know, that's the reason for the extension method in the first place, isn't it? But you simply can't use your extension method inside this query. One possible solution would be to directly query the table that contains the children. – Daniel Hilgarth Sep 05 '12 at 11:12