Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从集合的foreach循环返回值?_C#_Entity Framework_Asp.net Mvc 5 - Fatal编程技术网

C# 如何从集合的foreach循环返回值?

C# 如何从集合的foreach循环返回值?,c#,entity-framework,asp.net-mvc-5,C#,Entity Framework,Asp.net Mvc 5,我有两个类Invoice和InvoiceProductsInvoiceProducts是一个集合,有一个名为Price的get-only属性,我希望Invoice有一个名为TotalPrice的属性,它将返回添加到InvoiceProducts集合中的每个项目的Price。不过,我不确定是否会这样做。以目前的方式,我试图使用它,我得到一个错误,说 “对象引用未设置为对象的实例。”有办法做到这一点吗 当前方式: public class Invoice { public int Invoi

我有两个类
Invoice
InvoiceProducts
InvoiceProducts
是一个集合,有一个名为
Price
的get-only属性,我希望
Invoice
有一个名为
TotalPrice
的属性,它将返回添加到
InvoiceProducts
集合中的每个项目的
Price
。不过,我不确定是否会这样做。以目前的方式,我试图使用它,我得到一个错误,说

“对象引用未设置为对象的实例。”有办法做到这一点吗

当前方式:

public class Invoice
{
    public int InvoiceID { get; set; }
    public string ClientName { get; set; }
    public DateTime Date { get; set; } = DateTime.Today;
    private decimal totalPrice;
    public decimal TotalPrice {
        get
        {
            return totalPrice;
        }
        set
        {
            foreach(var item in InvoiceProducts)
            {
                totalPrice += item.Price;
            }
        }
    }

    public virtual ICollection<InvoiceProducts> InvoiceProducts { get; set; }
}

public class InvoiceProducts
{
    public int InvoiceProductsID { get; set; }
    public int InvoiceID { get; set; }
    public int ProductID { get; set; }
    public int ProductQuantity { get; set; }
    public decimal Price { get { return Product.ProductPrice * ProductQuantity; } }

    public virtual Invoice Invoice { get; set; }
    public virtual Product Product { get; set; }
}
公共类发票
{
public int InvoiceID{get;set;}
公共字符串ClientName{get;set;}
公共日期时间日期{get;set;}=DateTime.Today;
私人十进制总价;
公共十进制总价{
得到
{
返回总价;
}
设置
{
foreach(发票产品中的var项目)
{
总价+=项目价格;
}
}
}
公共虚拟ICollection InvoiceProducts{get;set;}
}
公共类发票产品
{
public int InvoiceProductsID{get;set;}
public int InvoiceID{get;set;}
public int ProductID{get;set;}
public int ProductQuantity{get;set;}
公共十进制价格{get{return Product.ProductPrice*ProductQuantity;}}
公共虚拟发票{get;set;}
公共虚拟产品产品{get;set;}
}
或者更短,因为它是get only:

public decimal TotalPrice => InvoiceProducts.Sum(product => product.Price);
当然,您需要初始化您的产品列表,并可能使其仅获得

public Invoice() 
{
    InvoiceProducts = new List<InvoiceProcuct>();
}

public ICollection<InvoiceProduct> InvoiceProducts { get; }
公共发票()
{
InvoiceProducts=新列表();
}
公共ICollection发票产品{get;}

我知道这已经得到了回答,但如果我可以尝试的话,我想提供一些额外的澄清。
set
接受参数,并且是一种特殊的逻辑,您希望在分配属性值时根据所分配的内容运行该逻辑。举一个简单的例子:

  public class SpecialNumber
    {
        private double _theNumber; 

        public double TheNumber
        {
            get { return _theNumber; }
            set
            {
                _theNumber = value * Math.PI;
            }
        }
    }
保留字
是表达式的右侧:

specialNumberObject.TheNumber = 5.0;
因此,在setter中,值将为5.0。S.Spindler指出,您在setter中拥有的逻辑非常适合于
获取
,因为它定义了一个自定义返回,您希望在我们想要访问属性值时执行该返回

我的类的另一个版本可能会决定在退出时使用一个与PI相乘的属性,如果我的类中的后端逻辑依赖于数字是它的“未乘法形式”,这可能会很有用。在这种情况下,逻辑将更适合getter


我希望我没有混淆这个问题。

为InvoiceProducts添加init(例如公共虚拟ICollection InvoiceProducts{get;set;}=new List();),您还可以删除TotalPrice的集合部分,并仅使用get(当然在返回前计算答案)可能的重复项
specialNumberObject.TheNumber = 5.0;