vb linq从两个数据表中选择使用左连接和分组并给出Sum

时间:2017-06-07 14:51:51

标签: vb.net linq linq-to-sql

我创建了一个linq,允许从tabRealisation左连接tabRealisation

中进行选择
 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"分组

1 个答案:

答案 0 :(得分: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()