C# 如何添加一个“;获取全部”;使用linq的方法

C# 如何添加一个“;获取全部”;使用linq的方法,c#,asp.net,asp.net-mvc,linq,entity-framework,C#,Asp.net,Asp.net Mvc,Linq,Entity Framework,我想在我的类中添加一个“get all”items方法。使用lineCollection,我可以看到(.find(),.findall(),.findindex()),但我认为这不是我需要的?有什么帮助吗 using System.Collections.Generic; using System.Linq; namespace SportsStore.Domain.Entities { public class Cart { private readonly

我想在我的类中添加一个“get all”items方法。使用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; }
    }
}
使用System.Collections.Generic;
使用System.Linq;
命名空间sportstore.Domain.Entities
{
公共类购物车
{
私有只读列表lineCollection=新列表();
公共可数线
{
获取{return lineCollection;}
}
公共无效附加项(产品、整数数量)
{
CartLine line=lineCollection
.Where(p=>p.Product.ProductID==Product.ProductID)
.FirstOrDefault();
如果(行==null)
{
添加(新的CartLine{Product=Product,Quantity=Quantity});
}
其他的
{
行.数量+=数量;
}
}
公共空隙清除线(产品)
{
lineCollection.RemoveAll(l=>l.Product.ProductID==Product.ProductID);
}
公共十进制计算总价值()
{
returnlinecollection.Sum(e=>e.Product.Price*e.Quantity);
}
公共空间清除()
{
lineCollection.Clear();
}
}
公共类CartLine
{
公共产品产品{get;set;}
公共整数数量{get;set;}
}
}

lineCollection已经是一个列表。只需返回列表即可获得所有元素。如果您想对这些元素执行某些操作,可以使用foreach循环。如果需要将IQueryable转换为列表,请使用.ToList()

lineCollection已经是一个列表。只需返回列表即可获得所有元素。如果您想对这些元素执行某些操作,可以使用foreach循环。如果需要将IQueryable转换为列表,请使用.ToList()