【发布时间】:2020-11-25 05:01:31
【问题描述】:
我想在 C# 中计算一个数组中的一个键,但我不知道如何
这是我的 json
{ "id": "1312312312", "customer_name": "Tatang Sutarman", "table_number": "12", "Cart": [ { "product_name": "Mineral Water", "amount": 3, "price": 1000, "subtotal": 3000 }, { "product_name": "Coca Cola", "amount": 5, "price": 2000, "subtotal": 10000 } ], "total_price": 0 }
这是我的对象类
public class OrderRRModel
{
public ObjectId id { get; set; }
public string customer_name { get; set; }
public string table_number { get; set; }
public IList<ListCart> Cart { get; set; }
public OrderRRModel()
{
Cart = new List<ListCart>();
}
public double total_price { get; set; }
}
public class ListCart
{
public string product_name { get; set; }
public int amount { get; set; }
public double price { get; set; }
public double subtotal { get; set; }
public double GetTotal()
{
var subtotal = amount * price;
return subtotal;
}
}
我想计算数组内的小计,结果我将放入total_price
你能告诉我怎么做吗???
【问题讨论】:
-
total_price = Cart.Sum(x => x.subtotal)? -
我想知道为什么您的数据中同时包含
amount和price以及subtotal。这是多余的。