Share via

How to get One column 'B' Distinct With Linq?

Mansour_Dalir 2,036 Reputation points
2023-06-05T17:35:36.1233333+00:00

hi I want an array of string type in Column 'B' thank

        Dim MyDataTable New DataTable
        MyDataTable.Columns.Add("A")
        MyDataTable.Columns.Add("B")
        MyDataTable.Rows.Add({"a", "w1"})
        MyDataTable.Rows.Add({"b", "w4"})
        MyDataTable.Rows.Add({"c", "w1"})
        MyDataTable.Rows.Add({"k", "w4"})
        MyDataTable.Rows.Add({"m", "w5"})
    Dim drSelect() As DataRow
        drSelect =MyDataTable.DefaultView.ToTable.Select("", "[B] asc")
Developer technologies | VB
0 comments No comments

Answer accepted by question author

Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
2023-06-06T01:42:53.3933333+00:00

Hi @Mansour_Dalir ,

You could refer to the following code.

  1. dtParent.AsEnumerable() converts the DataTable into an enumerable collection.
  2. Select(Function(row) row. Field(Of String)("B")) Select the value of column B.
  3. Distinct() returns an indistinct value.
  4. ToArray() converts the result to an array of strings.
        Dim distinctValues As String() = MyDataTable.AsEnumerable().Select(Function(row) row.Field(Of String)("B")).Distinct().ToArray()

Best Regards.

Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.