简体   繁体   English

从一个数据表中循环记录并在 vb.net 中填充另一个数据表

[英]Looping through records from one DataTable and populating another DataTable in vb.net

I have an asp.net application where I have a datatable ("DataTableA") that returns just one column ("ProductID").我有一个 asp.net 应用程序,其中我有一个仅返回一列(“ProductID”)的数据表(“DataTableA”)。

I want to read each row of "ProductID", process some business logic, then copy both those columns (ProductID & ProductIDBusinessLogicValue) to DataTableB.我想读取“ProductID”的每一行,处理一些业务逻辑,然后将这两列(ProductID 和 ProductIDBusinessLogicValue)复制到 DataTableB。 This DataTableB is then displayed on the asp.net page.此 DataTableB 然后显示在 asp.net 页面上。

What would bhe the best way to read each row of DataTableA?读取 DataTableA 每一行的最佳方法是什么?

Thanks谢谢

you can copy DataTableA to DataTableB, add column and do business logic on each row, something like this:您可以将 DataTableA 复制到 DataTableB,添加列并在每一行上执行业务逻辑,如下所示:

  DataTable dataTableB = dataTableA.Copy();
  dataTableB.Columns.Add("ProcessedValue", typeof(string));

  foreach (DataRow rw in dataTableB.Rows)
  {
    rw.SetField<string>("ProcessedValue", BusinessLogic(rw.Field<int>("ProductID")));
  }

Here's another approach you might like (linq)这是您可能喜欢的另一种方法(linq)

Dim table1 As DataTable = ds.Tables("DataTableA")    
Dim query = From product In table1.AsEnumerable() Select ProductID, myCalculatedField    
Dim table2 As DataTable = query.CopyToDataTable()

More Info...更多信息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM