Reputation: 51
I have an object list in a class library, I want to access a single part of the list which contains pricing and save it to a decimal property.
Below is the list being created just for context.
OrderItem ord = new OrderItem();
if (Quantity <= 0){ Quantity = 1; }
ord.ProductName = Product;
ord.Quantity = Quantity;
ord.LatestPrice = Price;
ord.TotalOrder = AddItems(Price, Quantity);
shopping.Add(ord);
The list gets displayed in a list box as well but I to take all the final decimals and create a total cost.
ShoppingListBox.Items.Clear();
foreach (OrderItem Product in shopping)
{
ShoppingListBox.Items.Add(string.Format("{0,-30}", Product.ProductName) + string.Format("{0,2}",Product.Quantity.ToString()) + string.Format("{0,15:C2}",Product.LatestPrice) + string.Format("{0,18:C2}",Product.TotalOrder) + Environment.NewLine);
}
I've looked at this for a while now and tried to avoid it for as much time as I can. I thought this post may have held my answers but only contains object arrays. Previous Thread
Upvotes: 1
Views: 100
Reputation: 3900
You can calculate total cost with LINQ method easily:
decimal totalCost = shopping.Sum(orderItem => orderItem.TotalOrder);
It will basically iterate over the shopping collection and sum TotalOrder
properties.
With that information you can use it and display it wherever you want
Upvotes: 1