2017-06-07 42 views
0

我已創建一個LINQ允許從tabRealisation選擇離開加入tabRealisationVB LINQ從兩個DataTable選擇使用留下了分組加入,並給予總

Dim query = 
    (From r In tabRealisation.AsEnumerable 
    Group Join p In tabPrevision.AsEnumerable 
    On r.Field(Of Integer)("code_part") Equals p.Field(Of Integer)("code_part") 
    Into Group 
    Let p = Group.FirstOrDefault 
    Select 
      nom_ville = r.Field(Of String)("nom_ville"), 
      Designation_part = r.Field(Of String)("Designation_part"), 
      NbrR = r.Field(Of Integer)("UniteVendu"), 
      CaR = r.Field(Of Double?)("CAVendu"),   
      NbrP = p.Field(Of Integer)("UniteVentePrev"), 
      CaP = p.Field(Of Double?)("CAVentePrev")  
      ).ToList 

但我想要的是把這個查詢得到Sum("UniteVendu")Sum("CAVendu") ......通過"code_part"

回答

0

分組試試這個

Dim query = 
     (From r In tabRealisation.AsEnumerable 
     Join p In tabPrevision.AsEnumerable 
     On r.Field(Of Integer)("code_part") Equals p.Field(Of Integer)("code_part") 
     Select 
       nom_ville = r.Field(Of String)("nom_ville"), 
       Designation_part = r.Field(Of String)("Designation_part"), 
       NbrR = r.Field(Of Integer)("UniteVendu"), 
       CaR = r.Field(Of Double?)("CAVendu"), 
       NbrP = p.Field(Of Integer)("UniteVentePrev"), 
       CaP = p.Field(Of Double?)("CAVentePrev"), 
       codePart = r.Field(Of Integer)("code_part") 
     ).GroupBy(Function(x) x.codePart).Select(Function(x) New With { _ 
                 .codepart = x.Key, 
                 .NbrRSum = x.Sum(Function(y) y.NbrR), 
                 .CarSum = x.Sum(Function(y) y.CaR) 
               ).ToList()