3

I am trying to retrieve all of the distinct values from a particular column in a datatable. The column name in the datatable is "Count". I have 2240 rows and I have 6 distinct values in the "Count" column. The problem is, when I execute the following code, it is giving me the number of rows rather than the 6 distinct values.

Dim counts = (From row In loadedData
Select row.Item("Count")).Distinct()
For Each i In counts
    MsgBox(i)
Next

How can I modify this to retrieve the 6 distinct values, rather than it giving me the total number of rows?

1
  • FYI - The values in this datatable are all unique... except that of the "Count" column. I believe this is why it is returning the 2240 rows, instead of just the 6 unique count values. Commented Oct 8, 2013 at 12:41

3 Answers 3

11

You just have to select the column and use Enumerable.Distinct:

Dim distinctCounts As IEnumerable(Of Int32) = loadedData.AsEnumerable().
    Select(Function(row) row.Field(Of Int32)("Count")).
    Distinct()

In query syntax(i didn't know that even Distinct is supported directly in VB.NET):

distinctCounts = From row In loadedData
                 Select row.Field(Of Int32)("Count")
                 Distinct
Sign up to request clarification or add additional context in comments.

2 Comments

I tried to use your code you posted, but I don't think you read my last comment. All 2240 are unique, however I am just trying to get the unique count column. "Select distinct count from datatable" - essentially.
@JohnJanssen: That is exactly the Linq-To-DataSet equivalent from your posted sql query. It returns only the distinct counts from the table. So if the table contains 2240 rows and the Count column contains 6 different numbers(spread over all rows), the query returns only 6 different numbers.
6

You can use ToTable(distinct As Boolean, ParamArray columnNames As String()) method for this.

loadedData.DefaultView.ToTable(True, "Count")

This will return distinct Users for you. You can add multiple column names if you want.

Here is the msdn documentation. https://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx

1 Comment

Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".
0

you can also apply this logic if you want , first sort datatable through columName , then apply this logic

    dtRecords.DefaultView.Sort = "columnName"
    dtRecords = dtRecords.DefaultView.ToTable
    Dim totalRecords As Integer = 0
    Dim thNameStr As String = filter(dtRecords, "columnName", totalRecords )

Public Shared Function filter(ByVal dtRecords As DataTable, ByVal columnName As String, ByRef totalRecords As Integer) As String
            Dim FilterStr As String = ""
            Dim eachTotal As Integer = 0
            totalRecords = 0
            Dim lastName As String = ""
            For rCount = 0 To dtRecords.Rows.Count - 1
                If lastName <> "" And lastName <> dtRecords.Rows(rCount)("" & columnName) Then
                    FilterStr &= lastName & " - [" & eachTotal & "]"
                    eachTotal = 0
                    totalRecords += 1
                End If
                lastName = dtRecords.Rows(rCount)("" & columnName)
                eachTotal += 1
            Next
            FilterStr &= lastName & " - [" & eachTotal & "]"
            totalRecords += 1
            Return FilterStr 
        End Function

1 Comment

A little bloated. That just seems like a lot of code to do the same thing that I can do in 3 lines of code with LINQ. The code doesn't seem wrong at all though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.