Reputation: 2293
I'm wanting to add a "get all" items method in my class. with lineCollection I can see the (.find(), .findall(), .findindex() ) but I don't think this is what I'm needing? Any help?
using System.Collections.Generic;
using System.Linq;
namespace SportsStore.Domain.Entities
{
public class Cart
{
private readonly List<CartLine> lineCollection = new List<CartLine>();
public IEnumerable<CartLine> Lines
{
get { return lineCollection; }
}
public void AddItem(Product product, int quantity)
{
CartLine line = lineCollection
.Where(p => p.Product.ProductID == product.ProductID)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new CartLine {Product = product, Quantity = quantity});
}
else
{
line.Quantity += quantity;
}
}
public void RemoveLine(Product product)
{
lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
}
public decimal ComputeTotalValue()
{
return lineCollection.Sum(e => e.Product.Price*e.Quantity);
}
public void Clear()
{
lineCollection.Clear();
}
}
public class CartLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
}
Upvotes: 0
Views: 1899
Reputation: 644
lineCollection is already a list. Just return List to get all the elements. If you want to do something with those elements, you can use a foreach loop. If you need to convert an IQueryable to a List, use .ToList()
Upvotes: 4