how do get arrays from head of group and subgroup with Linq?

Mansour_Dalir 1,716 Reputation points
2023-05-30T18:24:41.8133333+00:00

hi

Waiting for output

array1=(a1,a2,a3)

array2=(1-2,3-4,5)

Note: head group is [key] .thank


      Dim dt As New DataTable
        dt.Columns.Add("a")
        dt.Columns.Add("b")
        dt.Rows.Add("a1", "1")
        dt.Rows.Add("a1", "2")
        dt.Rows.Add("a2", "3")
        dt.Rows.Add("a2", "4")
        dt.Rows.Add("a3", "5")
        Dim dr() As DataRow
        dr = dt.Select()
        Dim drGroup = dr.GroupBy(Function(row) row("a"))
        'need array1=(a1,a2,a3)
        'need array2=(1-2,3-4,5)
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,661 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 29,026 Reputation points Microsoft Vendor
    2023-05-31T02:28:52.2666667+00:00

    Hi @Mansour_Dalir , You can refer to the following code, using Linq.

            Dim query = From row In dt.AsEnumerable()
                        Group row("b") By key = row("a") Into Group
                        Select New With {
                    .Key = key,
                    .Values = String.Join("-", Group.Select(Function(r) r.ToString()))
                }
    
            Dim array1 = query.Select(Function(item) item.Key).ToArray()
            Dim array2 = query.Select(Function(item) item.Values).ToArray()
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 29,026 Reputation points Microsoft Vendor
    2023-05-31T01:47:03.5033333+00:00

    Hi @Mansour_Dalir ,

    You can refer to the following code.

    Use dictionary key-value pairs to implement categorical storage data.

            Dim dt As New DataTable
            dt.Columns.Add("a")
            dt.Columns.Add("b")
            dt.Rows.Add("a1", "1")
            dt.Rows.Add("a1", "2")
            dt.Rows.Add("a2", "3")
            dt.Rows.Add("a2", "4")
            dt.Rows.Add("a3", "5")
            dt.Rows.Add("a3", "6")
    
            Dim dict As New Dictionary(Of String, List(Of String))()
            Dim array1 As New List(Of String)()
            Dim array2 As New List(Of String)()
    
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim key As String = dt.Rows(i)("a").ToString()
                Dim value As String = dt.Rows(i)("b").ToString()
    
                If dict.ContainsKey(key) Then
                    dict(key).Add(value)
                Else
                    dict(key) = New List(Of String) From {value}
                    array1.Add(key)
                End If
            Next
    
            For Each pair As KeyValuePair(Of String, List(Of String)) In dict
                array2.Add(String.Join("-", pair.Value))
            Next
    

    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.