0
我想從發票項目的總和中獲取表中每個發票的總計。在使用LINQ的主詳細信息中總結項目
我的發票數據模型是
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與這個Command相關聯,必須先關閉」
我希望如何解決這個問題。