Jim
Jim

Reputation: 29

LINQ to Entities does not recognize the method

public IQueryable<Product> GetModelsInBrand(int BrandId) 
{ 
    IQueryable<Product> query = 
         from Product 
          in ObjectContext.Products.Where(p => (p.BrandId == BrandId)) 
         orderby Product.Model 
         select Product; 
        query = query.Distinct(new ProductByModelEqualityComparer()); 
        return query; 
}

After return query was executed, I got

Load operation failed for query 'GetModelsInBrand'. LINQ to Entities does not recognize the method."

Can anyone help to correct it?

Upvotes: 2

Views: 1821

Answers (1)

Snowbear
Snowbear

Reputation: 17274

Probably LINQ-to-entities doesn't support the code which you've written in ProductByModelEqualityComparer. You can call AsEnumerable before calling Distinct, this will make Distinct executed via linq-to-objects but it won't be IQueryable anymore:

var enumerable = query.AsEnumerable().Distinct(new ProductByModelEqualityComparer()); return query; }

Upvotes: 3

Related Questions