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

Mansour_Dalir 1,996 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")
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,756 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,931 Reputation points Microsoft Vendor
    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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