I'm getting the error 'Product' is not supported in LINQ to Entities whilst using cartItems.Product.UnitCost when trying to get the total of the basket.
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;
}
I then tried splitting the query up to see if this would fix the problem
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();
I get the same problem with the second separate query with 'Product'.
Product Class
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; }
}
ShoppingBasket Class
public partial class ShoppingBasket
{
public int CartID { get; set; }
public string BasketID { get; set; }
public int BasketQuantity { get; set; }
public int ProductID { get; set; }
public virtual Product Product { get; set; }
}
Could someone explain the problem more clearly and how I can solve this? Thanks!
I think it's because Entity Framework hasn't fully created the relationship between ShoppingBasket
and Product
. Try adding:
public virtual ICollection<ShoppingBasket> ShoppingBaskets { get; set; }
to the Product
class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.