我正在为我的项目使用View / Presenter结构。该项目是一个包含OrderedItems和Products的电子通信站点。 当调用 GetOrdersItemsByOrderID(orderID)时,我收到LINQ错误。
LINQ to Entities无法识别方法'DAL.Views.ProductView GetProductBySku(System.String)'方法,而且此方法无法转换为商店表达式。
我知道LINQ不喜欢其查询中的dynaimc内容,因此proplem是SELECT 产品= Presenters.ProductsPresenter.GetProductBySku(c.productSKU)
我不知道如何构建GetOrdersItemsByOrderID TOLIST,同时还有另一个GetProductBySku方法填充字段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; }
}
/ ---------------- /
答案 0 :(得分:2)
您可以通过添加&#34; ToList&#34;在调用该方法之前执行查询。
试试这个:
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)
});
我将ToList()
向上移了一下。您可能仍然需要在Select的末尾添加一个。