vb.net从连接更新数据表列并与LINQ求和

时间:2016-03-25 14:23:43

标签: c# sql vb.net linq datatable

我有两个数据表是

dtOrder

 orderid  | trackno | totalamount
----------+--------+------------
     1    | test1   |    0
     2    | test2   |    0
     3    | test3   |    0
     4    | test4   |    0
     5    | test5   |    0
     6    | test6   |    0
     7    | test7   |    0
     8    | test8   |    0

dtAmounts

id | amount   | trackno | 
---+----------+--------+
 1 |     5    | test1   |   
 2 |    10    | test2   |   
 3 |    15    | test1   |   
 4 |    10    | test4   |   
 5 |    20    | test1   |   
 6 |    30    | test6   |   
 7 |    15    | test4   |   
 8 |    10    | test6   |   

我需要加入并根据曲目编号得到金额总和,因此得到的数据表应为:

 orderid  | trackno | totalamount
----------+--------+------------
     1    | test1   |    40
     2    | test2   |    10
     3    | test3   |    0
     4    | test4   |    25
     5    | test5   |    0
     6    | test6   |    40
     7    | test7   |    0
     8    | test8   |    0

如何使用LINQ和VB.net实现这一目标?

1 个答案:

答案 0 :(得分:0)

C#:

var req = (from Ord in dtOrder
           join Am in dtAmounts 
            on Ord.trackno 
                equals Am.trackno into tbResult
                select new
               {
                      orderid  = Ord.orderid   ,
                  trackno = Ord.trackno  ,
                   totalamount = cl.PrenomClient ,
                    amount   = Am.GetChildRows("amount").Sum()
                 }).ToList();

VB:

Dim req = (From Ord In dtOrderJoin Am In dtAmounts On Ord.trackno = Am.trackno Into tbResultNew With { _
Key .orderid = Ord.orderid, _
Key .trackno = Ord.trackno, _
Key .totalamount = cl.PrenomClient, _
Key .amount = Am.GetChildRows("amount").Sum()   }).ToList()