How to get Index, Column Names, values But Distinct ​​from DataTable to One Array As String With LINQ (Transpose Data)

Mansour_Dalir 1,996 Reputation points
2023-07-09T17:11:15.5333333+00:00

hi .Transpose data From Columns to Rows in Table .thank

        Dim MyDataTable As New DataTable 
        Dim OnlyColumns as String()={"Type","Location","Size"}
        MyDataTable.Columns.Add("Group")
        MyDataTable.Columns.Add("Type")
        MyDataTable.Columns.Add("Size")
        MyDataTable.Columns.Add("Location")
        MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "Air Building"})
        MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "ACC Building"})
        MyDataTable.Rows.Add({"CABLE", "LV", "1x240", "Swithgear"})
        MyDataTable.Rows.Add({"Install", "Cable Tray", "20", "Swithgear"})
        Dim ResultArray as String()'Need Linq Command to:
        ResultArray(0)="0,Type,MV,LV,Cable Tray"
        ResultArray(1)="1,Location,Air Building,ACC Building,Swithgear"
        ResultArray(2)="2,Size,1x240,20"

        


        ```

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

Accepted answer
  1. Jiachen Li-MSFT 33,121 Reputation points Microsoft Vendor
    2023-07-10T02:20:33.4166667+00:00

    Hi @Mansour_Dalir ,

    You can refer to the following code.

            Dim ResultArray As String() = Enumerable.Range(0, MyDataTable.Columns.Count) _
        .Select(Function(index) index & "," & MyDataTable.Columns(index).ColumnName & "," & String.Join(",", MyDataTable.AsEnumerable().Select(Function(row) row(index).ToString()).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.