这是一个经典问题,但我不知道如何在我的代码中弄明白。
对象引用未设置为对象的实例。
我知道我必须写一些像new IList<SubInvoice>();
这样的东西,但我不知道在哪里。
private void GetSub()
{
var invoice = Selected;
var invs = (from i in ctx.Invoices
join p in ctx.products on i.refSupp equals p.refsup
join u in ctx.units on p.unit equals u.idunit
where i.suppInvNumber == invoice.suppInvNumber
select new SubInvoice
{
Invoice = i,
description = p.description,
unit1 = u.unit1
});
if(Products != null)
Products.Clear();
if (invs != null)
foreach(var inv in invs)
{
SubInvoice sub = new SubInvoice(){Invoice=inv.Invoice, description = inv.description, unit1 = inv.unit1};
Products.Add(sub); //error
}
}
private IList<SubInvoice> _products;
public IList<SubInvoice> Products
{
get
{
return _products;
}
set
{
_products = value;
OnPropertyChanged("Products");
}
}
public class SubInvoice
{
public Invoice Invoice { get; set; }
public string description { get; set; }
public string unit1 { get; set; }
}
答案 0 :(得分:3)
private IList<SubInvoice> _products = new List<SubInvoice>()
答案 1 :(得分:3)
替换如下:
if(Products != null)
Products.Clear();
与
if(Products != null)
Products.Clear();
else
Products = new List<SubInvoice>();