-1

I need some help., i am using vb.net and javascript.

I have dataset like this,

Row     Price     ProductID     Region

1       100       12345         JK1

2       100       12345         JK2

3       100       23456         JK1

4       100       23456         JK2

i need the result JK1 = 200 and JK2 = 200. Result is SUM all price with grup by region. May be in SQL this is so simple, but in programming i haven't try.

I need the algoritm in vb.net or javascript. The problem is, the region have a lot of variation that i dont know how much that is.

can some one help?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Krichevskoy
  • 254
  • 2
  • 17

2 Answers2

0

DataTable.Compute Method can help you:

Dim sumPrice As Object = dataTable.Compute("Sum(Price)", "Region ='" + "JK1" + "'")
Eric
  • 248
  • 2
  • 8
  • 21
0

I like to use Dictionaries for this purpose. I don't really have much experience with datasets but it doesn't really matter for this example. I will just define input as a List(Of RegionPrice) with

Structure RegionPrice
  Dim Region as String
  Dim Price as Integer
End Structure
[...]
Dim Data as List(Of RegionPrice) = ....

Just loop through your data and get the Region and Price of the current item. Then

Dim dictSum as New Dictionary(Of String, Integer)
For i = 0 to Data.Count - 1
  If dictSum.ContainsKey(Data(i).Region) Then
    dictSum(Data(i).Region) += Data(i).Price
  Else
    dictSum.Add(Data(i).Region,Data(i).Price)
  End If
Next

You will then have the sums you want for each region in the Dictionary. Maybe that helps for these kinds of problems.

Jens
  • 6,275
  • 2
  • 25
  • 51