Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
   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:


C#
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.
Posted
Updated 17-Mar-18 12:34pm
v2
Comments
Dodido_72 17-Mar-18 19:01pm    
To clarify - I want to achieve this:
public virtual decimal ComputeTotalValue()=>
cartItemCollection.Sum(e => e.Item.Price * e.Quantity);
But because my item(aka product) has several prices I am stuck in
cartItemCollection.Sum(e => e.Item.Prices. * e.Quantity).
What shall I add to after Prices to get the value of the chosen instance so it can be multiplied with the quantity-value??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900