我想在我的班级中添加一个“获取所有”项目方法。使用 lineCollection 我可以看到 (.find(), .findall(), .findindex() ) 但我认为这不是我需要的?有什么帮助吗?
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; }
}
}