C# 如何在c中计算数组中的键#

C# 如何在c中计算数组中的键#,c#,arrays,json,C#,Arrays,Json,我想在C#中计算数组中的一个键,但我不知道如何计算 这是我的json { "id": "1312312312", "customer_name": "Tatang Sutarman", "table_number": "12", "Cart": [ { "product_name": "Mineral Water",

我想在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;
    }

}
公共类orderrmodel
{
公共对象id{get;set;}
公共字符串customer_name{get;set;}
公共字符串表_编号{get;set;}
公共IList购物车{get;set;}
公共秩序模型()
{
购物车=新列表();
}
公共双总价{get;set;}
}
公共类ListCart
{
公共字符串产品名称{get;set;}
公共整数金额{get;set;}
公共双价{get;set;}
公共双小计{get;set;}
公共双GetTotal()
{
var小计=金额*价格;
返回小计;
}
}
我想计算数组内的小计,并将结果放入总价中


你能告诉我怎么做吗?

在你的模型中更改此选项

public double total_price 
{ 
    set 
    {
        total_price = Cart.Sum(x => x.GetTotal());
    } 
    get;     
}

total\u price=Cart.Sum(x=>x.subtotal)
?我想知道为什么您的数据中既有
金额
又有
价格
,还有
小计
。这是多余的。你的意思可能是
获取{return Cart.Sum(x=>x.GetTotal());}
,或者?谢谢,我得到了总价,但我如何得到小计???您仍然可以使用相同的方法在CartList模型中设置{subtotal=amount*price;}并使用Cart.Sum(x=>x.subtotal);在OrderRRModel模型中,get如何?public double subtotal{get;set{subtotal=amount*price;}}}它显示错误“get必须声明一个主体,因为它没有标记为抽象或外部”
public double subtotal{get{return amount*price;}