vb.net - 从数据表中乘以值

时间:2015-07-07 19:46:16

标签: sql asp.net vb.net datatable multiplying

我在vb.net工作,在我的程序中我有两个数据表。我想要的是将第二个数据表中某些IZ的数量乘以第一个数据表中相同IZ的数量。我想为每一个IZ做到这一点。

例如对于IZ12554,它将像1 * 460 = 460和0,0025 * 460 = 1,15;对于IZ10836,它将是3 * 2000 = 6000; 1 * 2000 = 2000; 1 * 2000 = 2000等,并将这些结果写入数据表3。

Capacity IZ Quantity ID Document ------------------------------------------- DM00006 IZ12554 460 20 PDN00084625 DM00007 IZ10836 2000 1 PDN00084983 DM00010 IZ12641 25200 1 PDN00084997 DM00011 IZ03941 940 1 PDN00084639 DM00087 IZ15137 50 12 PDN00084904 .... .... DATATABLE 2

IZ MA Quantity Unit ---------------------------------- IZ12554 MA06196 1 KOS IZ12554 MA05545 0,0025 KOS IZ10836 MA06199 3 KOS IZ10836 MA06197 1 KOS IZ10836 MA00892 1 kos IZ12641 MA06199 1 KOS IZ12641 MA32200 0,04 m IZ03941 MA07639 0,0025 KOS IZ03941 MA00896 0,295 M IZ03941 MA06200 1 KOS IZ03941 MA00892 1 kos IZ15137 MA06200 1 KOS IZ15137 MA00557 0,0025 KOS IZ15137 MA00897 0,295 M ... ... DATATABLE 3 - RESULTS IZ MA Quantity Unit ---------------------------------- IZ12554 MA06196 460 KOS IZ12554 MA05545 1,15 KOS IZ10836 MA06199 6000 KOS IZ10836 MA06197 2000 KOS IZ10836 MA00892 2000 kos IZ12641 MA06199 25200 KOS IZ12641 MA32200 1000,8 m IZ03941 MA07639 2,35 KOS IZ03941 MA00896 277,3 M IZ03941 MA06200 940 KOS IZ03941 MA00892 940 kos IZ15137 MA06200 50 KOS IZ15137 MA00557 0,125 KOS IZ15137 MA00897 14,75 M ... ... {{1}}

我搜索了答案,但仍然不知道如何使用数据表,我是一个完整的编程初学者......

提前致谢!

2 个答案:

答案 0 :(得分:1)

你可以尝试类似的东西:

DataTable DataTable3 = new DataTable();

DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "IZ";
DataTable3.Columns.Add(myDataColumn);

DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "MA";
DataTable3.Columns.Add(myDataColumn);

DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Int32");
myDataColumn.ColumnName = "Sum_QTY";
DataTable3.Columns.Add(myDataColumn);
//ADD More columns to DataTable Here

foreach(DataRow row in DataTable1.Rows){
 foreach(DataRow row2 in DataTable2.Rows){
   if (row["IZ"].ToString()==row2["IZ"].ToString()){
    //Row3 is the row from Table 3
    row3 = DataTable3.NewRow();

    row3["IZ"]=row["IZ"].ToString();
    row3["MA"]=row2["MA"].ToString();
    row3["Sum_QTY"]=row2["Quantity"]*row["Quantity"];
    //ADD calculations here and add them to row3
    DataTable3.Rows.Add(row3);
  }
 }
}

答案 1 :(得分:1)

正如对该问题的评论所示,在数据库中执行此操作可能会更有效,但您可以使用LINQ to DataSet来加入数据并执行计算:

Dim result =
    From r2 In dt2.AsEnumerable()
    Join r1 In dt1.AsEnumerable() On r2.Field(Of String)("IZ") Equals r1.Field(Of String)("IZ")
    Select
        IZ = r2.Field(Of String)("IZ"),
        MA = r2.Field(Of String)("MA"),
        Quantity = r2.Field(Of Decimal)("Quantity") * r1.Field(Of Integer)("Quantity"),
        Unit = r2.Field(Of String)("Unit")

这会为您提供包含所需结果的IEnumerable个匿名类型。我不知道将内容转换回DataTable的内置方法(如果你需要的话),但你可以稍微反思一下。有关详细信息,请参阅this question,但此类内容应该有效:

<Extension>
Function ToDataTable(Of T)(items As IEnumerable(Of T)) As DataTable
    Dim dt = New DataTable()
    Dim props = GetType(T).GetProperties()
    For Each prop In props
        Dim dc = dt.Columns.Add(prop.Name, prop.PropertyType)
    Next
    For Each item In items
        Dim dr = dt.NewRow()
        For Each prop In props
            dr(prop.Name) = prop.GetValue(item)
        Next
        dt.Rows.Add(dr)
    Next
    Return dt
End Function

然后你可以:

Dim dt3 = result.ToDataTable()
' or ToDataTable(result) if you don't make it an extension method