Need to check. I get to the error LINQ

Mansour_Dalir 2,016 Reputation points
2023-07-27T17:18:49.8866667+00:00

hi

     Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim MyDataTable As New DataTable
        MyDataTable.Columns.Add("id", GetType(Integer))
        MyDataTable.Columns.Add("A")
        MyDataTable.Columns.Add("B")
        MyDataTable.Columns.Add("C")
        MyDataTable.Rows.Add({1, "MV", "1x240", "Air Building"})
        MyDataTable.Rows.Add({2, "MV", "1x240", "ACC Building"})
        MyDataTable.Rows.Add({3, "LV", "1x240", "Swithgear "})
        MyDataTable.Rows.Add({4, "Cable Tray", "20", "Swithgear"})
        MyDataTable.Rows.Add({5, "Cable Tray", "30", "ACC"})
        MyDataTable.Rows.Add({6, "Cable Tray", "60", "ACC"})
        MyDataTable.Rows.Add({7, "JB", "10x10", "Admin"})
        MyDataTable.Rows.Add({8, "JB", "20x10", "Admin"})
        MyDataTable.Rows.Add({9, "Detector", "Smoke", "Admin"})
        MyDataTable.Rows.Add({10, "Detector", "Beam", "ACC"})
        MyDataTable.Rows.Add({11, "Detector", "Heat", "ACC"})
        Dim columName As String = "A"
        Dim q = From row In MyDataTable.AsEnumerable()
                Group row("id") By key = row(columName) Into Group
                Select New With {.Key = CStr(key.ToString()), .Values = Group.Select(Function(r) r).ToArray}
        Dim GroupKey = q.Select(Function(item) item.Key).ToArray
        Dim GroupValues As Integer()() = q.Select(Function(item) item.Values.ToArray)' This Line Error
    End Sub

GroupValues = (0)={1,2} (1)={3} (2)={4,5,6} (3)={7,8} (4)={9,10,11}

Then how to convert two-dimensional array to one-dimensional?

dim ArrayOneDimensional as integer()=GroupValues

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.7K Reputation points
    2023-07-27T18:43:35.8066667+00:00

    Do you mean this array?

    Dim GroupValues As Integer() = q.SelectMany(Function(item) item.Values.Select(Function(v) CInt(v))).ToArray
    

1 additional answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 33,451 Reputation points Microsoft Vendor
    2023-07-28T02:59:14.97+00:00

    Hi @Mansour_Dalir ,

    If you want to get IDs as Integer(), you can refer the following code.

            Dim columName As String = "A"
            Dim q = From row In MyDataTable.AsEnumerable()
                    Group row("id") By key = row(columName) Into Group
                    Select New With {
                .Key = CStr(key.ToString()),
                .Values = String.Join(",", Group.Select(Function(r) CStr(r)))
            }
            Dim GroupKey = q.Select(Function(item) item.Key).ToArray
            Dim GroupIds As Integer() = MyDataTable.AsEnumerable().Select(Function(row) CInt(row("id"))).ToArray()
            'result= {1,2,3,4,5,6,7,8,9,10,11}
            Dim GroupValues As String() = q.Select(Function(item) item.Values).ToArray()
            'result= {"1,2","3","4,5,6","7,8","9","10,11"}
    

    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.


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.