使用LINQ对主要细节中的项目求和

时间:2013-02-18 10:30:52

标签: asp.net-mvc linq-to-sql entity-framework-5

我试图从发票项目的总和中获取表格中每张发票的总数。

我的发票数据模型是

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必须先关闭”

我很感激如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

似乎我需要的是添加

MultipleActiveResultSets = TRUE;

在我的连接字符串中。