Reputation: 349
I have a datatable in a vb.net Project that it is filled from a stored procedure, the data inside the dataTable is like the below:
Division Department Unit ID
---------- ---------- ---------- -----
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Development User 1
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Management User 2
Division a Business Operations Office Support User 3
Division a Business Operations Office Support User 3
Division a Business Operations Office Support User 3
I want to compute the count of how many unit i have, and the occurrence of it
example: here i have 3 units, (11 development, 10 management and 3 office support)
Upvotes: 1
Views: 3849
Reputation: 56
One possible way to solve is to do like this:
Dim distinctDT As DataTable = dt.DefaultView.ToTable(True, "unit")
Msgbox(distinctDT.Rows.Count)
Where dt is your datatable
Upvotes: 0
Reputation: 460158
Easy with Linq-To-DataTable
:
Dim unitGroups = From row In table.AsEnumerable()
Group row By unit = row.Field(Of String)("Unit") Into Group
Select unit = New With {.Name = unit, .Count = Group.Count()}
Order By unit.Count Descending
For Each grp In unitGroups
Console.WriteLine("Unit: {0} Count:{1}", grp.Name, grp.Count)
Next
The Order By
is not needed, i wanted to show it anyway. If you want to know the number of units you just need:
Dim countUnits As Int32 = unitGroups.Count()
Upvotes: 1