Ravi Ram
Ravi Ram

Reputation: 24488

Linq Select with inner method error - LINQ to Entities does not recognize the method

I am using a View / Presenter structure for my project. The project is a e-comm site with OrderedItems and Products. When calling GetOrdersItemsByOrderID(orderID), I get an LINQ error.

LINQ to Entities does not recognize the method 'DAL.Views.ProductView GetProductBySku(System.String)' method, and this method cannot be translated into a store expression.

I know that LINQ does not like dynaimc content within its query so the proplem is the SELECT Product = Presenters.ProductsPresenter.GetProductBySku(c.productSKU)

I dont know how to Build the GetOrdersItemsByOrderID TOLIST while also have another GetProductBySku method filling the field ProductDetail

/----------------/

public static List<OrderItemsView> GetOrdersItemsByOrderID(int orderID)
{
    using (var ctx = new ProductEntities())
{

    var result = ctx.OrderedItems.Where(o => o.orderID == orderID)
        .Select(c => new OrderedItemsView
                         {
                             OrderItemID = c.orderItemID,
                             OrderID = c.orderID,
                             ProductSku = c.productSKU,
                             ProductPrice = c.productPrice,
                             ProdQuantity = c.prodQuantity,
                             ProductDetail = Presenters.ProductsPresenter.GetProductBySku(c.productSKU)
                         }).ToList();
    return result;
    }
}

/----------------/

public class OrderedItemsView
    {
        public int OrderItemID { get; set; }
        public int OrderID { get; set; }
        public string ProductSku { get; set; }
        public decimal ProductPrice { get; set; }
        public int ProdQuantity { get; set; }
        public decimal? ProductWeight { get; set; }

      public ProductView ProductDetail { get; set; } <-- Get Product Details
    }

/----------------/

public static Views.ProductView GetProductBySku(string sku)
    {
    using (GroupProductEntities ctx = new GroupProductEntities())
    {
        //-------------------------------------------------------------------//
        var results =
            ctx.Products.Where(p => p.clientID == Config.ClientID && p.productSKU == sku)
            .Select(p => new Views.ProductView
                             {
                                 ProductID = p.productID,
                                 ClientID = p.clientID,
                                 CategoryID = p.categoryID,
                                 ProductName = p.productName,
                                 ProductImage1 = p.productImage1,
                                 ProductPrice = p.productPrice,
                                 ProductInventory = p.productInventory,
                                 ProductSku = p.productSKU,
                             }).FirstOrDefault();
        return results;
    }
 }

/----------------/

public class ProductView
{
    public int ProductID { get; set; }
    public int? CategoryID { get; set; }
    public string ProductName { get; set; }
    public string ProductShortDesc { get; set; }
    public string ProductImage1 { get; set; }
    public decimal? ProductPrice { get; set; }
    public decimal? ProductInventory { get; set; }
    public string ProductSku { get; set; }
}

/----------------/

Upvotes: 0

Views: 1120

Answers (1)

Robaticus
Robaticus

Reputation: 23157

You might be able to get away with adding a "ToList" to execute the query before you call that method.

Try this:

   var result = ctx.OrderedItems.Where(o => o.orderID == orderID).ToList()
        .Select(c => new OrderedItemsView
                         {
                             OrderItemID = c.orderItemID,
                             OrderID = c.orderID,
                             ProductSku = c.productSKU,
                             ProductPrice = c.productPrice,
                             ProdQuantity = c.prodQuantity,
                             ProductDetail = Presenters.ProductsPresenter.GetProductBySku(c.productSKU)
                         });

I moved your ToList() up a bit. You may still need one at the end of the Select as well.

Upvotes: 2

Related Questions