Linq Select with inner method error - LINQ to Entities does not recognize the method
我正在为我的项目使用视图/演示者结构。该项目是一个拥有ordereditems和产品的电子通信站点。调用GetOrderSiteMsbyorderID(orderID)时,我得到一个Linq错误。
linq to entities无法识别方法"dal.views.ProductView GetProductBysku(system.string)"方法,并且此方法无法转换为存储表达式。
我知道Linq不喜欢其查询中的dynaimc内容,所以proplem是selectproduct=presenters.productsPresenter.getProductBysku(c.productsku)
我不知道如何构建getordersitemsbyorderid tolist,同时还有另一个getproductbysku方法填充字段productdetail
/----------------/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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; } } |
/----------------/
1 2 3 4 5 6 7 8 9 10 11 | 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 } |
/----------------/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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; } } |
/----------------/
1 2 3 4 5 6 7 8 9 10 11 | 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; } } |
/----------------/
在调用该方法之前,您可能可以通过添加"tolist"来执行查询。
试试这个:
1 2 3 4 5 6 7 8 9 10 | 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) }); |
我把你的