2016-09-29 3 views
1

내 신청서에 대해 한 달 동안 인보이스를 집계하려고합니다. 두 가지 객체가 있습니다. Invoice 개체 및 InvoiceDetails 개체가 있습니다. Invoice 개체에는 송장 (날짜, 고객 등)에 대한 세부 정보가 들어 있고 InvoiceDetails 개체에는 각 송장의 수량 (수량, 가격, 세금 등)이 포함되어 있습니다. 송장에서 오는 돈을 기준으로 매월 수익을 계산하고 싶습니다. GroupBy 방법을 사용하여 날짜별로 송장을 집계 할 수 있지만 InvoiceDetails에 해당 월의 총 수익을 줄 수있는 광고 항목을 추가 할 수 없습니다. 아래에 개체가 나열되어 있습니다.LINQ Sum 하위 요소에 대한 쿼리

송장 클래스 :

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 

InvoiceDetails 클래스 :

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 

기능은 두 번째 마지막 줄 때까지 잘 보인다. 'Total'의 오류가 'InvoiceDetail'의 멤버가 아닙니다. invd은 자식 요소를 참조하지만 대신 부모 요소를 참조합니다. 이 LINQ 쿼리를 어떻게 작동시킬 수 있습니까?

+1

그냥 메모를 도움이 될 것입니다 다음

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) }) 

희망을 수행 할 수 있습니다 당신이 .AddSeconds은 (-1)'당신은'i.InvoiceDate을 <사용할 수있는'하지 않았다 경우 EndDate'와 초의 분수에 대해 걱정할 필요가 없습니다. 23 : 59 : 59.95에 계산 된 송장은 다음날 포함되어야합니다. –

+0

특정 연도와 특정 월의 송장 합계를 계산하고자한다고 생각합니까? 'Invoice'는'InvoiceDetails'와 어떻게 연결되어 있습니까? - 'Invoice' 및'InvoiceDetails'의 선언은 구두 설명보다 더 많은 도움이 될 수 있습니다. –

+0

'InvoiceDetails'는'Invoice'의 하위 오브젝트들의 모임입니다. 인보이스에는 'InvoiceDetails'와 같은 항목이 있습니다. 각 광고 항목에는 수량 및 가격과 함께 품목 설명이 있습니다. 한 달 동안 각 광고 항목의 가격을 합산하려고합니다. – Nse

답변