C# LINQ to实体中不支持指定的类型成员“Product”
在尝试获取篮子总数时,使用cartItems.Product.UnitCost时,我发现LINQ to Entities中不支持“Product”错误C# LINQ to实体中不支持指定的类型成员“Product”,c#,asp.net-mvc,linq,entity-framework,C#,Asp.net Mvc,Linq,Entity Framework,在尝试获取篮子总数时,使用cartItems.Product.UnitCost时,我发现LINQ to Entities中不支持“Product”错误 public decimal GetTotal() { //Multiply price by count of item to get price for each item in cart and them sum all prices up decimal? total = (from cartIte
public decimal GetTotal()
{
//Multiply price by count of item to get price for each item in cart and them sum all prices up
decimal? total = (from cartItems in db.ShoppingBaskets where cartItems.BasketID == ShoppingCartID select (int?)cartItems.BasketQuantity * cartItems.Product.UnitCost).Sum();
return total ?? decimal.Zero;
}
然后我尝试将查询拆分,看看这是否能解决问题
int? quantity = (from cartItems in db.ShoppingBaskets where cartItems.BasketID == ShoppingCartID select (int?)cartItems.BasketQuantity).Sum();
decimal? price = (from cartItems in db.ShoppingBaskets where cartItems.BasketID == ShoppingCartID select cartItems.Product.UnitCost).Sum();
我对第二个单独的查询“Product”也有同样的问题
产品类别
public partial class Product
{
public Product()
{
this.OrderCheckouts = new HashSet<OrderCheckout>();
this.ProductReviews = new HashSet<ProductReview>();
}
public int ProductID { get; set; }
public int CategoryID { get; set; }
public int ManufacturerID { get; set; }
public string ProductName { get; set; }
public string ProductCode { get; set; }
public decimal UnitCost { get; set; }
public int UnitQuantity { get; set; }
public string ProductDescription { get; set; }
public string ProductImage { get; set; }
public virtual Category Category { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual ICollection<OrderCheckout> OrderCheckouts { get; set; }
public virtual ICollection<ProductReview> ProductReviews { get; set; }
}
有人能更清楚地解释这个问题吗?我该如何解决这个问题?谢谢 我认为这是因为实体框架没有完全建立购物篮和产品之间的关系。尝试添加:
public virtual ICollection<ShoppingBasket> ShoppingBaskets { get; set; }
到Product类。为什么要转换为可为null的?您是否希望数量或价格为空?若有,为什么?这个消息让我觉得产品并没有从db.ShoppingBaskets转换成实体框架。不是产品的具体问题。你能用ShoppingBasket编辑你的问题吗?我只是想看看转换为nullable是否能解决这个问题。谢谢你的回复,不幸的是仍然得到同样的错误。@user3015121产品在你的应用程序中的其他地方工作吗?例如,db.Products有效吗?
public virtual ICollection<ShoppingBasket> ShoppingBaskets { get; set; }