Hi!
I am trying to add a product with many prices to a shopping cart(a one-to-many relationship entity framework). The customer has chosen a special size with its price in the product presentation (a short list with buttons on the right). Now I want to add this selected product, size and price to my shopping cart. The problem is - I don't know how to access the specific price (the particular value in the ItemPrice-list).
To start with - here are my classes and my DbContext:
public class Item
{
private readonly ICollection<ItemPrice> Price;
public Item(ICollection<ItemPrice> price = null)
{
Price = price;
}
public Item() : this(null) { }
[Key]
public int ItemID { get; set; }
[Required(ErrorMessage = "Please enter an product name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please specify a category")]
public string Category { get; set; }
public string SubCategory { get; set; }
public string Description { get; set; }
public string ImageSmallUrl { get; set; }
public string ImageSmallAlternativeDescription { get; set; }
public string ImageSmallContentType { get; set; }
public virtual ICollection<ItemPrice> Prices { get; set; }
}
public class ItemPrice
{
[Key]
public int ID { get; set; }
public int ItemID { get; set; }
public string Size { get; set; }
public decimal Value { get; set; }
public virtual Item Item { get; set; }
}
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext
(DbContextOptions<ApplicationDbContext>
options)
:base(options) {}
public DbSet<Item> Items { get; set; }
public DbSet<ItemPrice> ItemPrices { get; set; }
protected override void OnModelCreating (ModelBuilder modelBuilder)
{
modelBuilder.Entity<ItemPrice>()
.HasOne(p => p.Item)
.WithMany(b => b.Prices);
}
}
I have started to work on the shopping cart and the cartItem but now I am stuck. I don't know how to continue. Here are the ShoppingCart and the CartItem classes:
public class ShoppingCart
{
private List<CartItem> cartItemCollection = new List<CartItem>();
public virtual void AddCartItem(Item item, int quantity)
{
CartItem cartItem = cartItemCollection
.Where(p => p.Item.ItemID == item.ItemID)
.FirstOrDefault();
if(cartItem == null)
{
cartItemCollection.Add(new CartItem
{
Item = item,
Quantity = quantity
});
}
else
{
cartItem.Quantity += quantity;
}
}
public virtual void RemoveCartItem(Item item) =>
cartItemCollection.RemoveAll(l => l.Item.ItemID == item.ItemID);
public virtual decimal ComputeTotalValue()
{
return cartItemCollection.Sum(e => e.Item.Prices.) *
e.Quantity);
}
}
public class CartItem
{
public int CarItemID { get; set; }
public Item Item { get; set; }
public int Quantity { get; set; }
}
I have tried different lambda-expression and LINQ but I don't get them to work. How do I get the particular value (one of the properties of the ItemPrice-class) so I can retrieve a sum. Very grateful for help here!
What I have tried:
I have searched the web for two days and cannot find a good answer to the problem and, of course, tried a lot of different approaches in code.