关于join:在Vb.net中动态地向现有DataTable添加一列

Dynamically Adding a Column to existing DataTable in Vb.net

我正在使用Vb.Net Project,通过Web Service调用方法在DataTable的Object中获取结果,现在我需要在本地计算后添加更多列,我在进行所有循环并通过循环逐列添加数据或对其进行修改 ,对于较大的DataTable来说,这太花时间了。
是否有任何逻辑我可以一遍又一遍地遍历每个DataRow来添加这些列?

假设我有一个包含4列的DataTable,(名称= dt)
我需要再添加两个。

对于2000行,我必须为每一行去初始化新添加列的值。

****假设我在临时表中计算了新的暂定列。
有没有什么办法可以让我通过联接来更新新添加的列(添加到dt中)的值?这些表(在VB.NET代码内部)基于公共列(主键列)****


Suppose I have calculations of new tentative columns into a Temp
table. Is There any way so I can update the values of newly added
columns (added into dt)by joins The tables (inside VB.NET code) on the
bases of a common column (Primary Key Column)

如果tempTbl与主表位于同一数据集中(包含数据),并且您具有1:1匹配键关系:是的,可以。

在数据集中的两个表之间添加一个数据关系,并使用它来检索组合的数据行,其中包含所有相关表的列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    '   the variables
    Dim DSet As DataSet = New DataSet("DSet")
    Dim DTbl1 As DataTable = New DataTable("One")
    Dim DTbl2 As DataTable = New DataTable("Two")
    Dim DRelation As DataRelation

    '   setting up sample tables
    DTbl1.Columns.Add("SaleID", GetType(Integer))
    DTbl1.Columns.Add("ProductName", GetType(String))
    DTbl1.Columns.Add("AmountSold", GetType(Double))
    DTbl1.Columns.Add("ItemPrice", GetType(Double))

    DTbl2.Columns.Add("SaleID", GetType(Integer))
    DTbl2.Columns.Add("Turnover", GetType(Double))

    '   host this DataTables in the DataSet
    DSet.Tables.Add(DTbl1)
    DSet.Tables.Add(DTbl2)

    '   this is the exiting part: adding primary keys...
    '   the DataTable.PrimaryKey-property is an Array of DataRow, so I just initialize a new array containing the one column I would like to set as primary key for this table.
    DTbl1.PrimaryKey = {DTbl1.Columns("SaleID")}
    DTbl2.PrimaryKey = {DTbl2.Columns("SaleID")}

    '   ...and the DataRelation
    DRelation = New DataRelation("SaleIDRelation", DSet.Tables("One").Columns(0), DSet.Tables("Two").Columns(0))
    DSet.Relations.Add(DRelation)

    '   populate Tbl1 with some sample data
    DTbl1.Rows.Add(1,"Eggs", 4, 0.2)
    DTbl1.Rows.Add(2,"Apples", 5, 0.5)
    DTbl1.Rows.Add(3,"Milk", 5, 1)

    '   do the calculation
    For Each DRow As DataRow In DSet.Tables("One").Rows
        '   I personally prefer to keep iteration variables scope inside the loops, so the variable can get catched by the GarbegeCollector as soon as the loop is left
        Dim tPrice As Double = 0

        '   I also prefer not to rely on implicit conversion
        tPrice = Convert.ToDouble(DRow("AmountSold")) * Convert.ToDouble(DRow("ItemPrice"))

        '   for each row processed by the loop, add a row to the second table to store the calculations result
        '   this row should have the same SaleID, so the DataReleation will be able to relate the two rows together later on
        DTbl2.Rows.Add(DRow("SaleID"), tPrice)
    Next

    '   so now you'll be able to get the according DataRow(s) of the second table by retriving the depending ChildRows through the DataRelation
    For Each DRow As DataRow In DSet.Tables("One").Rows
        Console.WriteLine(String.Format("Product {0} in SaleID {1} has made a total turnover of {2}", DRow("ProductName"), DRow("SaleID"), DRow.GetChildRows("SaleIDRelation")(0)("Turnover")))
    Next

输出:

1
2
3
Product Eggs in SaleID 1 has made a total turnover of 0,8
Product Apples in SaleID 2 has made a total turnover of 2,5
Product Milk in SaleID 3 has made a total turnover of 5

真正的魔力发生在输出循环中。 我正在访问第一个子行的期望值,因为由于1:1 DataRelation,我已经确保Tbl1中的每个DataRow在Tbl2中都有一个具有相同SaleID的书。

所以我要做的是DRow.GetChildRows("SaleIDRelation")(0)("Turnover")

  • 对于DRow(此DataRow)
  • 使用名为"SaleIDRelation"的DataRelation获得相关的ChildRows
  • 使用找到的第一个ChildRow,以(0)表示
  • 在该DataRow中,我想获取列的值("营业额")