2013-02-18 59 views
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相关联,必须先关闭”

我希望如何解决这个问题。

回答

0

似乎我需要的是添加

MultipleActiveResultSets = true;

在我的连接字符串。