Vb.net 子元素上的LINQ Sum查询

Vb.net 子元素上的LINQ Sum查询,vb.net,entity-framework,linq,sum,aggregate,Vb.net,Entity Framework,Linq,Sum,Aggregate,我正试图在一个月内为我的申请汇总发票。有两个对象;Invoice对象和InvoiceDetails对象。Invoice对象包含有关发票的详细信息(日期、客户等),而InvoiceDetails对象包含每个发票的行项目(数量、价格、税务等)。我想根据发票上的收入来计算每个月的收入。我可以使用GroupBy方法按日期汇总发票,但是我似乎无法在InvoiceDetails中添加行项目,这将为我提供当月的总收入。下面列出了这些对象 发票类别: Public Class Invoice Publ

我正试图在一个月内为我的申请汇总发票。有两个对象;
Invoice
对象和
InvoiceDetails
对象。
Invoice
对象包含有关发票的详细信息(日期、客户等),而
InvoiceDetails
对象包含每个发票的行项目(数量、价格、税务等)。我想根据发票上的收入来计算每个月的收入。我可以使用
GroupBy
方法按日期汇总发票,但是我似乎无法在
InvoiceDetails
中添加行项目,这将为我提供当月的总收入。下面列出了这些对象

发票类别:

Public Class Invoice

    Public Property InvoiceID As Integer
    Public Property InvoiceDate As Date
    Public Property DueDate As Date
    Public Property Details As List(Of InvoiceDetail)
    Public Property Client As Client
    Public Property ClientID As Integer

End Class
发票详细信息类:

Public Class InvoiceDetail

    Public Property InvoiceDetailID As Integer
    Public Property Description As String
    Public Property UnitPrice As Decimal
    Public Property Quantity As Integer
    Public Property Subtotal As Decimal
    Public Property Tax As Decimal
    Public Property Total As Decimal
    Public Property Invoice As Invoice

End Class
我当前的LINQ查询如下:

Public Function GetRevenueByMonth(Year As Integer, Month As Integer) As DataSourceResult

    Dim StartDate As New Date(Year, Month, 1)
    Dim EndDate As Date = StartDate.AddMonths(1).AddSeconds(-1)

    Dim Revenue = _db.Invoices.Include(Function(i) i.InvoiceDetails
        .Select(Function(invd) invd.Total))
        .Where(Function(i) i.InvoiceDate >= StartDate And i.InvoiceDate <= EndDate)
        .GroupBy(Function(i) i.InvoiceDate.Date)
        .Select(Function(r) New With {
            .Date = r.Key, .Revenue = r.Sum(Function(invd) invd.Total)
        })

End Function
公共函数GetRevenueByMonth(年为整数,月为整数)作为数据源结果
Dim StartDate作为新日期(年、月、1)
Dim EndDate As Date=开始日期。添加月份(1)。添加秒(-1)
Dim收入=_db.Invoices.Include(功能(i)i.invoicesdetails
.选择(功能(投资总额)投资总额)

.Where(Function(i)i.InvoiceDate>=StartDate和i.InvoiceDate您可以执行以下操作

 Dim Revenue = _db.InvoicesDetails
        .Where(Function(i) i.Invoice.InvoiceDate >= StartDate And i.Invoice.InvoiceDate <= EndDate)
        .GroupBy(Function(i) i.Invoice.InvoiceDate.Date)
        .Select(Function(r) New With {
            .Date = r.Key, .Revenue = r.Sum(Function(invd) invd.Total)
        })
Dim收入=\u db.InvoicesDetails

.Where(函数(i)i.Invoice.InvoiceDate>=StartDate和i.Invoice.InvoiceDate只是一个注释:如果您没有
.AddSeconds(-1)
那么你可以使用
i.InvoiceDate
而不必担心几秒钟。除非第二天应该包括一张时间为23:59:59.95的发票。我认为你想得到特定年份和月份的发票总额是正确的吗?
invoice
如何连接到
InvoiceDetails
?-
Invoice
InvoiceDetails
的声明可能对我们的帮助不仅仅是口头描述。
InvoiceDetails
Invoice
的子对象的集合。发票有一个行项目集合,即
InvoiceDetails
的集合。每个行项目都有一个项目描述,一个详细介绍了数量和价格。我想汇总当月各行项目的价格。对不起,如果没有逐字代码声明,就不容易准确理解您的意思。如果您将代码写成
\u db.Invoices.形式。选择(函数(inv As Invoice)inv.InvoiceDate>=startDate和also inv.InvoiceDate
使用
作为
术语,可能有助于了解错误所在。@AndrewMorton我已更改代码以包含类的声明。