没看懂你的意思。这样?
Dim dic As new Dictionary(of String,Integer)
'定义字典,键类型为字符串,值类型为整数
Dim rc As Integer = DataTables("生产计划").DataRows.Count-1
DataTables("采购需求").DataRows.Clear()
'清空采购需求表
Dim i As Integer
For i = 0 To rc
Dim dr1 As DataRow
dr1 = DataTables("生产计划").DataRows(i)
'循环生产计划中的每一行数据
For Each dr2 As DataRow In DataTables("商品结构").Select("[父级编码] = '" & dr1("商品编码") & "'")
'以集合的形式返回符合条件的数据,并遍历每一行,而不是傻傻的遍历表中所有数据了.
Dim ly As DataRow = DataTables("商品资料").Find("[商品编码] = '"& dr2("子级编码")&"'")
If ly("来源")="自产" Then
Dim ziji As DataRow = DataTables("生产计划").find("商品编码 = '" & dr2("子级编码") & "' and 数量 = '" & dr1("数量") * dr2("数量") & "'")
If ziji Is Nothing Then
ziji = DataTables("生产计划").AddNew
ziji("商品编码")=dr2("子级编码")
ziji("数量")=dr1("数量") * dr2("数量")
End If
rc=rc+1
ElseIf ly("来源")="外购" Then
Dim nm As String = dr2("子级编码")
'此处数据类型要跟前面字典的相符
Dim vl As Integer = dr1("数量") * dr2("数量")
'直接计算出相应物料的采购需求数量
If dic.ContainsKey(nm) = False
dic.Add(nm,vl)
'如果字典中不存在相应的物料,则新增,否则累加
Else
dic(nm) = dic(nm)+ vl
End If
End If
Next
Next
For Each key As String In dic.keys
'遍历字典中所有的键的集合
Dim dr As DataRow = DataTables("采购需求").AddNew()
'增加新的数据行
dr("商品编码") = key
dr("需求数量") = dic(key)
Next