求高手解决下Datatbale问题,急!!!!!!!!!!!!!!!

淡定的新手 2011-04-15 11:52:57
我创建了以下Datatable
Dim Currtable As DataTable
Currtable.Columns.Add("Curr")
Currtable.Columns.Add("Number")
Dim csDataRow As DataRow = Currtable.NewRow()
csDataRow.Item("Curr") = “A”
csDataRow.Item("Number") = 123

Dim csDataRow As DataRow = Currtable.NewRow()
csDataRow.Item("Curr") = “B”
csDataRow.Item("Number") = 11

Dim csDataRow As DataRow = Currtable.NewRow()
csDataRow.Item("Curr") = “A”
csDataRow.Item("Number") = 100

Dim csDataRow As DataRow = Currtable.NewRow()
csDataRow.Item("Curr") = “B”
csDataRow.Item("Number") = 123

Dim csDataRow As DataRow = Currtable.NewRow()
csDataRow.Item("Curr") = “C”
csDataRow.Item("Number") = 100

我需要将这个
我要循环去 “Curr” 列中的数据
如果是“A”
那么我就将 “Number”中的数据去中来累加
如果是 “B”
那么我就将 “Number”中的数据去中来累加

最后面 。我要计算出 属于 “A” 的总额
最后面 。我要计算出 属于 “B” 的总额

还有。在程序中。“Curr” 列的值。不是很明确 不一定等于“A”或者“B” 还有其他的可以能
...全文
63 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2011-04-15
  • 打赏
  • 举报
回复
用DataTable.Compute:
Sub Main()
Dim objDt As New DataTable
objDt.Columns.Add("Curr")
objDt.Columns.Add("Number", GetType(integer))
objDt.Rows.Add(New Object() {"A", 100})
objDt.Rows.Add(New Object() {"A", 100})
objDt.Rows.Add(New Object() {"A", 100})
objDt.Rows.Add(New Object() {"B", 100})
objDt.Rows.Add(New Object() {"B", 100})
objDt.Rows.Add(New Object() {"C", 100})

Dim totalOfA As Integer = objDt.Compute("SUM(Number)", "Curr='A'")
Console.WriteLine("TotalOfA:{0}", totalOfA)

Dim totalOfB As Integer = objDt.Compute("SUM(Number)", "Curr='B'")
Console.WriteLine("TotalOfB:{0}", totalOfB)
'
Dim totalOfC As Integer = objDt.Compute("SUM(Number)", "Curr='C'")
Console.WriteLine("TotalOfC:{0}", totalOfC)

Console.Read()
End Sub
q107770540 2011-04-15
  • 打赏
  • 举报
回复
linq to datatable
count统计

Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim products As DataTable = ds.Tables("Product")

Dim query = _
From product In products.AsEnumerable() _
Group product By color = product.Field(Of String)("Color") Into g = Group _
Select New With {.Color = color, .ProductCount = g.Count()}

For Each product In query
Console.WriteLine("Color = {0} " & vbTab & "ProductCount = {1}", _
product.Color, _
product.ProductCount)
Next

http://msdn.microsoft.com/zh-cn/library/bb669075(v=vs.90).aspx
淡定的新手 2011-04-15
  • 打赏
  • 举报
回复
具体的使用方法是怎样的?
Peter200694013 2011-04-15
  • 打赏
  • 举报
回复
Select
http://msdn.microsoft.com/en-us/library/det4aw50.aspx

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧