2010-12-02 25 views
1

Ok Je sais qu'il y a beaucoup de messages à ce sujet, mais j'ai essayé de résoudre ce problème pendant quelques heures et mon cerveau est complètement éteint.Problème VB.NET LINQ Rejoindre le groupe

Voici la chose

J'ai une liste d'objets avec 3 propriétés (quantité, service, nom)

Je veux avoir tous les noms et services dans ma liste (cross peu rejoindre) et la quantité groupée de la ligne correspondante. Pas clair, je suppose

Quantité Nom du service
3 srv2 Bob
4 srv2 Paul
2 Srv1 Paul
1 srv2 Nick

Je veux en sortie
Tous les services et tous les noms et correspondant quantités (0 si aucun)

Srv1 Paul 2
Srv1 Bob 0
Srv1 Nick 0
srv2 Paul 4
srv2 Bob 3
srv2 Nick 1

Voici ce que je suis arrivé à ce jour, mais je ne même obtenir les résultats attendus Et je suis acutally certain qu'il est assez facile manière de parvenir à ce que je veux ... Je peu d'aide serait la bienvenue ...

Merci

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 

Répondre

0

Je l'ai ici est mon résultat, il y a sûrement une meilleure façon, mais de toute façon, il fonctionne

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