How do I create a dynamic function? 'Linq'

Mansour_Dalir 2,036 Reputation points
2023-06-14T05:27:01.4233333+00:00

hi It is possible that my table is dynamic. with variable number of columns. Thanks for the correction (Function FilterByChildTable)

    Private Sub test10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dtParent As New DataTable
        Dim dtChild As New DataTable
        Dim dtResult As New DataTable
        dtParent.Columns.Add("parentId", GetType(Integer))
        dtParent.Columns.Add("words")
        dtParent.Rows.Add({1, "w1"})
        dtParent.Rows.Add({2, "w2"})
        dtParent.Rows.Add({3, "w3"})
        dtParent.Rows.Add({4, "w4"})
        dtParent.Rows.Add({5, "w5"})

        dtChild.Columns.Add("chidId", GetType(Integer))
        dtChild.Rows.Add(4)
        dtChild.Rows.Add(3)
        dtChild.Rows.Add(2)

        dtResult = FilterByChildTable(dtParent, dtChild)
        Dim dtParent2 As New DataTable
        dtParent2.Columns.Add("parentId", GetType(Integer))
        dtParent2.Columns.Add("words")
        dtParent2.Columns.Add("other words")
        dtParent2.Rows.Add({1, "w1", "Other"})
        dtParent2.Rows.Add({2, "w2", "Other"})
        dtParent2.Rows.Add({3, "w3", "Other"})
        dtParent2.Rows.Add({4, "w4", "Other"})
        dtParent2.Rows.Add({5, "w5", "Other"})
        dtResult = FilterByChildTable(dtParent2, dtChild,True)
    End Sub
    Private Function FilterByChildTable(DynamicColumn As DataTable, StaticColumn As DataTable,Optional WithZASort as Boolean=false) As DataTable
         'WithZASort False is AZ        
         'DynamicColumn is: with variable number of columns
        'StaticColumn is: with a fixed number of columns

        Dim query = DynamicColumn.AsEnumerable() _
               .Join(StaticColumn.AsEnumerable(),
                     Function(parentRow) parentRow("parentId").ToString(),
                     Function(childRow) childRow("chidId").ToString(),
                     Function(parentRow, childRow) New With {
                         .Filter = parentRow("parentId"),'Static Column
                         .Words = parentRow("words")'Dynamic Column
                     }) _
               .OrderBy(Function(row) row.Filter)
        Dim resultTable As New DataTable("Result") ' Dynamic
        'resultTable.Columns.Add("parentId") 'Static Column
        'resultTable.Columns.Add("words") 'Dynamic Column
        'And Other Column 'Dynamic Column
        Dim dtResult As New DataTable
        dtResult = query.Select(Function(row) resultTable.Rows.Add("need Dynamic Code")).CopyToDataTable
        Return dtResult
    End Function
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,822 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,206 Reputation points Microsoft External Staff
    2023-06-14T07:01:17.89+00:00

    Hi @Mansour_Dalir ,

    You can use the following code to query a DataTable with a dynamic number of columns.

       Private Function FilterByChildTable(DynamicColumn As DataTable, StaticColumn As DataTable, Optional WithSort As Boolean = False) As DataTable
    
            Dim dynamicColumnNames = DynamicColumn.Columns.Cast(Of DataColumn)().Skip(1).Select(Function(col) col.ColumnName).ToArray()
    
            Dim query = DynamicColumn.AsEnumerable() _
                   .Join(StaticColumn.AsEnumerable(),
                         Function(parentRow) parentRow("parentId").ToString(),
                         Function(childRow) childRow("chidId").ToString(),
                         Function(parentRow, childRow) New With {
                             .Filter = parentRow("parentId"), ' Static Column
                             .DynamicData = dynamicColumnNames.Select(Function(colName) parentRow(colName)).ToArray() ' Dynamic Columns
                         }) _
                   .OrderBy(Function(row) row.Filter)
    
            Dim resultTable As New DataTable("Result") ' Dynamic
            resultTable.Columns.Add("parentId") ' Static Column
            For Each colName In dynamicColumnNames
                resultTable.Columns.Add(colName) ' Dynamic Columns
            Next
    
            Dim dtResult As DataTable = resultTable.Clone() ' Create a clone of resultTable
    
            For Each row In query
                Dim newRow = dtResult.NewRow()
                newRow("parentId") = row.Filter
    
                For i As Integer = 0 To dynamicColumnNames.Length - 1
                    newRow(i + 1) = row.DynamicData(i)
                Next
    
                dtResult.Rows.Add(newRow)
            Next
    
            If WithSort Then
                dtResult.DefaultView.Sort = "parentId ASC" ' Sort dtResult by parentId column in ascending order
                dtResult = dtResult.DefaultView.ToTable()
            End If
    
            Return dtResult
        End Function
    

    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.

2 additional answers

Sort by: Most helpful
  1. Mansour_Dalir 2,036 Reputation points
    2023-06-14T16:19:06.0533333+00:00

    hi @Jiachen Li-MSFT

    Very very easy . Thank. I am becoming a professional.

        Private Function FilterByChildTable2(DynamicColumn As DataTable, StaticColumn As DataTable) As DataTable
            Dim query =
                From sel In DynamicColumn.AsEnumerable()
                Where StaticColumn.AsEnumerable().Select(Function(Helper) Helper.Item("chidId")).Contains(sel.Item("parentId"))
                Order By sel.Field(Of Integer)("parentId")
                Select sel 
            Return query.CopyToDataTable() 'view.ToTable
        End Function
    

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.