2016-05-18 9 views
0

Ich habe eine Abfrage, um alle Aufträge mit Details zu erhalten. Ich habe eine Abfrage wie folgt aus:kann keinen anderen Service in Select-Methode aufrufen-Linq

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(); 

Entsprechend dass * ich ProductcombinationName brauchen zu bekommen, dies zu tun, rufe ich Methode in einem anderen Dienst, aber diesen Fehler:

LINQ to Entities does not recognize the method 

erste Idee ist, fügen foreach für alle Zeilen und rufen Sie diese Methode auf ProductCombinationName, aber ich weiß nicht, ist es ein guter Weg?

Antwort

2

Sie können nicht C# -Funktionen in linq verwenden, da linq versuchen, Code auf SQL-Seite auszuführen, wo SQL nicht versteht, was Ihre C# -Funktion ist und Fehler geben.
dafür,
Sie können wie tun

    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(), 

und dann

foreach(var item in Items) 
{ 
    // add ProductCombinationName code here 
} 
1

Entity Framework wird nicht C# -Code als Teil seiner Abfrage ausführen, es muss in der Lage sein, um die Abfrage zu konvertieren zu einem tatsächlichen SQL Ausdruck.

Also müssen wir Ihren Abfrageausdruck in einen Ausdruck umstrukturieren, den Entity Framework verarbeiten kann.

Verwandte Themen