Click here to Skip to main content
14,177,047 members
Rate this:
Please Sign up or sign in to vote.
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) { }

    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
    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
        :base(options) {}

    public DbSet<Item> Items { get; set; }
    public DbSet<ItemPrice> ItemPrices { get; set; }

    protected override void OnModelCreating (ModelBuilder modelBuilder)
            .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)
        if(cartItem == null)
            cartItemCollection.Add(new CartItem
                Item = item,
                Quantity = quantity
            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.) *


 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.
Updated 17-Mar-18 12:34pm
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190526.1 | Last Updated 17 Mar 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100