简体   繁体   English

如何使用 c# 中 2 个类的对象进行计算?

[英]How to make calculations with objects from 2 classes in c#?

Here are Models:以下是型号:

public class Order_Items
{
    public int orderId { get; set; }
    public int itemId { get; set; }
    public int quantity { get; set; }
    public double itemPrice { get; set; }
}     

public class Items 
{
    [Key]
    public int id { get; set; }
    public String name { get; set; }
    public int quantity { get; set; }
    public double price { get; set; }
    public String text { get; set; }
}

My question is how can I set the value of itemPrice by calculating Order_Items.quantity * Items.price.我的问题是如何通过计算 Order_Items.quantity * Items.price 来设置 itemPrice 的值。

Put a reference to Items class on Order_Items class在 Order_Items class 上引用项目 class

Public Items Item {get;set;}

Then change the ItemPrice property to getter only with the following然后将 ItemPrice 属性更改为仅使用以下内容的 getter

Public double ItemPrice=>Item.Price*quantity;

Note that you must set the item property and to avoid NullReferenceException on reading请注意,您必须设置 item 属性并避免在读取时出现 NullReferenceException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM