如何计算总成本

How to calculate total cost

我正在尝试通过 Entity Framework 6 Code First 使用 MVC 5 构建我的第一个应用程序,但我有一个问题,我的应用程序是包含项目 class 的在线餐厅,它包含 itemprice 和 itemamount我想计算它的价值 itemprice*itemamount 以保存在订单的总成本中 class,我如何在初始化程序中做到这一点[在此处输入图像描述][1]???

var Items = new List<MenuItem>
     {
         new MenuItem { ItemName = "Cheese Piza", Itemdesrciption="Pizza with cheese", MenuID=int.Parse("3"), ItemPrice=Decimal.Parse("15.5"), ItemAmount=int.Parse("2")},
         new MenuItem { ItemName = "Bolinez Pasta", Itemdesrciption="pasta with red sous", MenuID=int.Parse("3"),ItemPrice=Decimal.Parse("20.45"), ItemAmount=int.Parse("3")},
         new MenuItem { ItemName = "Holy Cake", Itemdesrciption="Cake with choklet sous", MenuID=int.Parse("3"), ItemPrice=Decimal.Parse("45.5"), ItemAmount=int.Parse("1")},
     };

     Items.ForEach(I => context.Item.Add(I));
        context.SaveChanges();




        var Orderss = new List<Order>
     {
         new Order { CustomerID = int.Parse("1"), ItemID=int.Parse("3"), OrderDate= DateTime.Parse("2005-09-01"),
             TotalPrice = },

     };

只需使用var price = Items.Sum(t => t.ItemPrice); 但是,我猜你必须大量处理这段代码:)

Abaout int.parse read this

您可以使用计算属性添加计算列。这应该创建一个在数据库中计算的价格列。私有集是为了让程序无法覆盖数据库提供的计算。

public class MenuItem 
{
    [Key]
    public Int MenuID{ get; set; }
    public string ItemName { get; set; }
    public string Itemdesrciption { get; set; }
    public Int ItemAmount{ get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]      
    public string ItemPrice
    {
        get { return /* do your sum here */ }
        private set { /* needed for EF */ }
    }
}

我认为数据没有必要保存在数据库中,因为它是一个计算字段,我想你可以给我们展示一下你的数据库模型,观察你的逻辑,它是像master detail这样的东西,其中老师可以是 "Order" table 并且 table 详细说明了 "order Details" 订单的项目,还有一个 table 用于可用项目 "Items" .

    namespace Model
{

    public class Order
    {
        public int Id { get; set; }

        public int CustomerId { get; set; }

        [ForeignKey( "CustomerId" )]
        public virtual Customer Customer { get; set; }

        public virtual List<OrderDetail> Details { get; private set; }

        public double Total { get { return Details.Sum( t => t.SubTotal ); } }

        public Order()
        {
            this.Details = new List<OrderDetail>();
        }
    }

    public class OrderDetail
    {
        public int Id { get; set; }
        public int OrderId { get; set; }
        public virtual Order Order { get; set; }

        public int ItemId { get; set; }

        [ForeignKey( "ItemId" )]
        public virtual Item Item { get; set; }

        public int Quantity { get; set; }

        public double SubTotal { get { return this.Item.Price * this.Quantity; } }
    }

    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public double Price { get; set; }
        public double Cost { get; set; }
    }
}

namespace Try
{
    public class Program
    {
        public static void Main()
        {
            using (DataBaseContext context = new DataBaseContext())
            {
                context.Items.AddOrUpdate( t => t.Name, 
                    new Model.Item() { Name = "MyProduct1", Price= 14.5, Cost=11.5, Description="" } ,
                    new Model.Item() { Name = "MyProduct2", Price = 16.5, Cost = 14.5, Description = "" },
                    new Model.Item() { Name = "MyProduct3", Price = 20.5, Cost = 18.5, Description = "" },
                    new Model.Item() { Name = "MyProduct4", Price = 10.5, Cost = 8.5, Description = "" }
                    );

                context.Savechanges();


                Model.Order Order= new Model.Order() { CustomerId=3};

                Order.Details.AddRange( new Model.OrderDetail[] {
                new Model.OrderDetail {  ItemId= 1, Quantity=2},
                new Model.OrderDetail {  ItemId= 2, Quantity=1},
                new Model.OrderDetail {  ItemId= 3, Quantity=5},
                new Model.OrderDetail {  ItemId= 4, Quantity=6}
                } );

                Console.WriteLine( "Total: {0} for {1} Items", Order.Total, Order.Details.Count );
                Console.ReadLine();
            }               

        }
    }
}

请启用 Lazy 以自动加载导航属性。