我试图从发票项目的总和中获取表格中每张发票的总和。
我的发票数据模型是
public class Invoice
{
[Key]
public int InvoiceId { get; set; }
public int ClientId { get; set; }
public int CustomerId { get; set; }
public string Type { get; set; }
public string Number { get; set; }
public DateTime Date { get; set; }
public bool Paid { get; set; }
public bool Printed { get; set; }
public string Notes { get; set; }
public virtual ICollection<InvoiceItem> InvoiceItems { get; set; }
public virtual Customer customer { get; set; }
}
我的 invoiceitem 数据模型是
public class InvoiceItem
{
[Key]
public int InvoiceItemId { get; set; }
public int InvoiceId { get; set; }
[StringLength(100)]
public string PartNo { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public int TaxId { get; set; }
public virtual Invoice Invoice { get; set; }
}
要填充我有的网格
response.Records.Add(new JqGridRecord(Convert.ToString(x.Invoice.InvoiceId), new InvoiceEditViewModel()
{
Id = x.Invoice.InvoiceId,
CustomerName = x.Customer.Name,
Total = x.Invoice.InvoiceItems.Where(p => p.InvoiceId == x.Invoice.InvoiceId).Sum(d => d.Price * d.Quantity ),
InvType = x.Invoice.Type,
Notes = x.Invoice.Notes,
Date = x.Invoice.Date.ToShortDateString(),
Number = x.Invoice.Number,
Printed = x.Invoice.Printed,
}));
}
但是,这在计算总数时会引发错误
“已经有一个打开的 DataReader 与此命令关联,必须先关闭”
我将不胜感激如何解决这个问题。