Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The environment consists of ASP.Net Core3.0,EF Core3.0,SQL Server. The GetProductsForCategory(id) retrieves all Products in a Category based on categoryID.This method calls GetRecord() which returns an entity of ProductAndCategory type.The models that are present are Product and Category(with navigation propery present) and a viewmodel ProductandCategory.The tables present are Category and Product. While executing this statement the following error is thrown->
Quote:
$exception {"The LINQ expression 'DbSet<product>\r\n .Where(p => p.CategoryId == __id_0)\r\n .Join(\r\n outer: DbSet<category>, \r\n inner: p => EF.Property<nullable<int>>(p, \"CategoryId\"), \r\n outerKeySelector: c => EF.Property<nullable<int>>(c, \"ID\"), \r\n innerKeySelector: (o, i) => new TransparentIdentifier<product, category="">(\r\n Outer = o, \r\n Inner = i\r\n ))\r\n .OrderBy(p => ProductRepo.GetRecord(\r\n p: p.Outer, \r\n c: p.Inner).ModelName)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().

The code block is ->
internal ProductAndCategoryBase GetRecord(Product p, Category c)
     => new ProductAndCategoryBase()
     {
       CategoryName = c.CategoryName,
       CategoryId = p.CategoryId,
       CurrentPrice = p.CurrentPrice,
       Description = p.Description,
       IsFeatured = p.IsFeatured,
       ID = p.ID,
       ModelName = p.ModelName,
       ModelNumber = p.ModelNumber,
       ProductImage = p.ProductImage,
       ProductImageLarge = p.ProductImageLarge,
       ProductImageThumb = p.ProductImageThumb,
       TimeStamp = p.TimeStamp,
       UnitCost = p.UnitCost,
       UnitsInStock = p.UnitsInStock
     };

   public IEnumerable<ProductAndCategoryBase> GetProductsForCategory(int id)
    => Table.Where(p => p.CategoryId == id)
       .Include(p => p.Category)
       .Select(item => GetRecord(item, item.Category)).OrderBy(x => x.ModelName);


The GetRecord() is used as it needs to be used in other methods in a similar manner and to avoid code duplication. I would like to know if its possible to resolve the error in this approach or an alternate approach is there to fix the code.

What I have tried:

From my understanding the statement is evaluated to be of form IQueryable. The expression that is created would not be able to resolve the GetRecord().But I do not have a clarity on how to proceed further.
Posted
Updated 18-Jan-22 7:09am
Comments
[no name] 18-Jan-22 12:53pm    
It says you need to do more than just returning a "where":

... call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().

1 solution

You can't do that because, behind the scenes, the LINQ query is being translated into an SQL query. Your call to your GetRecord method cannot be called by the SQL Server when retrieving records.

If you insist on using that method, you have to execute the query first to return the records WITHOUT the call to GetRecord and converting it to a List<>, then iterate over that list of returned records and call GetRecord on each item to project them into your ProductAndCategoryBase object.
 
Share this answer
 
Comments
rajkiran.07 19-Jan-22 2:43am    
That was an eye opener. Thank you, Dave.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900