0

I have a query to get all Orders with detail . I have a query like this :

 return
           _orderMasters.OrderByDescending(row => row.Code).Where(row => row.Code == code).Include(row => row.Orderdetails)
             .Select(row => new OrderViewModel
             {
                 Code = row.Code,
                 // other fields
                 Items = row.Orderdetails.Select(detail => new OrderItemViewModel
                 {
                     ProductcombinationId = detail.ProductCombinationId,
                     ProductId=detail.ProductAttributeCombination.ProductId,
                     ProductName = detail.ProductCombination.Product.Name,
                     ProductCombinationName=_productCombinationService.GetCombinationsName(detail.ProductCombinationId,detail.ProductCombination.ProductId) // * this row
                 }).ToList(),
                 ShippingAddress = new AddressViewModel()
                 {
                     //set fileds value
                 }
             }).FirstOrDefault();

In line that * I need get ProductcombinationName , to do this I call Method in another Service , but getting this Error :

LINQ to Entities does not recognize the method

First Idea Is , add foreach for All rows and call that Method to get ProductcombinationName , But I don't know is it a good way ?

Uthman Rahimi
  • 708
  • 5
  • 22

2 Answers2

1

you can not use c# functions into linq because linq try to execute code on sql side where sql doesn't understand what your c# function is and give error.
for this,
you can do like

                 Items = row.Orderdetails.Select(detail => new OrderItemViewModel
                 {
                     ProductcombinationId = detail.ProductCombinationId,
                     ProductId=detail.ProductAttributeCombination.ProductId,
                     ProductName = detail.ProductCombination.Product.Name,
                     ProductCombination = detail.ProductCombination // need to add for next operation
                 }).ToList(),

and then

foreach(var item in Items)
{
     // add ProductCombinationName code here
}
Pranav Patel
  • 1,541
  • 14
  • 28
1

Entity Framework will not run C# code as part of its query, it has to be able to convert the query to an actual SQL expression.

So we will have to restructure your query expression into an expression that Entity Framework can handle.

var orders = _orderMasters.OrderByDescending(row => row.Code)
                          .Where(row => row.Code == code)
                          .Include(row => row.Orderdetails)
                          .ToList();


return   orders.Select(row => new OrderViewModel
         {
             Code = row.Code,
             // other fields
             Items = row.Orderdetails.Select(detail => new OrderItemViewModel
             {
                 ProductcombinationId = detail.ProductCombinationId,
                 ProductId=detail.ProductAttributeCombination.ProductId,
                 ProductName = detail.ProductCombination.Product.Name,
                 ProductCombinationName=_productCombinationService.GetCombinationsName(detail.ProductCombinationId,detail.ProductCombination.ProductId) // * this row
             }).ToList(),
             ShippingAddress = new AddressViewModel()
             {
                 //set fileds value
             }
         }).FirstOrDefault();
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35