0

数据表1

Name   Quantity
A       2
B       5
C       3
D       4

数据表2

  Name   Quantity
    A       1
    B       3
    F       4
    G       4

加入两个数据表后的预期结果按名称分组,然后为每个组求和数量,将结果放入新数据表中

 Name   Quantity
    A       3
    B       8
    C       3
    D       4
    F       4
    G       4

使用 LINQ 可以做到这一点吗?请注意,我将字段值访问为row.Field(of T)("Name")

4

1 回答 1

1

试试这个。如果两张表都相同,我认为您需要 Union

Dim query = From x in ((From p in DataTable1
                Select p).Union(From pd in DataTable2 Select pd))
                Group x By Key = x.Name Into grouping = Group
                let myProdSum = grouping.Sum(Function(x) => x.Quantity)
                Select New With {.Name = grouping.Key, .TotalQty = myProdSum}

ForEach s in query 
  Console.WriteLine("{0} : {1}" , s.Name, s.TotalQty)
Next
于 2012-12-07T20:20:58.787 回答