2010-12-02 5 views
1

좋아요. 많은 게시물이 있지만 몇 시간 동안이 문제를 해결하려고 노력하고 있으며 내 뇌는 완전히 벗어났습니다. 여기 문제 VB.NET LINQ 그룹 합류

내가 3 곳 (양, 서비스, 이름)

내 목록에있는 모든 이름과 서비스를 (좀 가입 교차) 갖고 싶어

과와 객체의 목록을 가지고있는 것은

입니다 해당 행의 그룹화 된 수량 내가

수량 서비스 이름
3 SRV2 밥
4 SRV2 폴
2 SRV1 폴
1 SRV2 닉 내가 출력으로
모든 서비스와 모든 이름을 원하는

대응을 생각하지 명확 수량 (없는 경우 0)

SRV1 폴 2
SRV1 밥 0
,SRV1 닉 0
SRV2 한 다음

내가 지금까지 가지고,하지만 난 심지어 결과 를 예상 얻을 해달라고 그리고 나는 아주 쉽게이 acutally 확신 무엇 폴 4
SRV2 밥 3
SRV2 닉 난 ... 난 약간의 도움은 환영받을 원하는 것을 달성하는 방법 ...

감사

Dim services = (From a In interventions Select New With {.Service = a.Service}).Distinct() 
    Dim months = (From b In interventions Select New With {.Month = b.DateHeureIntervention.Month}).Distinct() 

    'Dim query = (From s In services _ 
    '    From m In months _ 
    '    From i In interventions.Where(Function(x) x.Service = s.Service And x.DateHeureIntervention.Month = m.Month).DefaultIfEmpty(New Intervention()) _ 
    '    Select New ChartItem With {.Service = s.Service, _ 
    '          .Name = MonthName(m.Month), _ 
    '          .Quantite = i.Quantite}).ToList() 

    'Return (From q In query _ 
    '  Group By srv = q.Service, n = q.Name Into Group _ 
    '  Select New ChartItem With {.Name = n, _ 
    '         .Service = srv, _ 
    '         .Quantite = Group.Sum(Function(i) i.Quantite)}).ToList() 


    Dim q = (From i In interventions _ 
      Group Join s In services On s.Service Equals i.Service Into si = _ 
      Group Join m In months On m.Month Equals i.DateHeureIntervention.Month _ 
      From x In si.DefaultIfEmpty() _ 
      Group By m = i.DateHeureIntervention.Month, srv = i.Service Into Group _ 
      Select New ChartItem With {.Service = srv, _ 
             .Name = MonthName(m), _ 
             .Quantite = Group.Sum(Function(y) y.i.Quantite)}).ToList() 

    Return q 

답변

0

나는 확실히 더 좋은 방법이있다, 그것은 여기 내 결과 가지고 있지만, 어쨌든, 그것은 작동

Dim months As List(Of Integer) = (From b In interventions Select b.DateHeureIntervention.Month).Distinct().ToList() 
    Dim services As List(Of String) = (From c In interventions Select c.Service).Distinct().ToList() 

    Dim q = (From s In services _ 
      From m In months _ 
      Select New ChartItem With {.Name = MonthName(m), _ 
             .Service = s, _ 
             .Quantite = (From i In AccesDonneesHelper.GetInterventionsDetails() _ 
                Where i.Service = s And i.DateHeureIntervention.Month = m _ 
                Select i.Quantite).Sum()}).ToList()