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.