How to use the function in the expression that adds a column in the data table

Mansour_Dalir 1,936 Reputation points
2024-07-28T06:39:37.5533333+00:00
 Dim MyTable As New DataTable
 MyTable.Columns.Add("Name")
 MyTable.Columns.Add("Age", GetType(Integer))
 MyTable.Rows.Add("Ali", 9)
 MyTable.Rows.Add("Amir", 15)
 MyTable.Rows.Add("Mansour", 18)
 MyTable.Columns.Add("isOk", GetType(Boolean), "IIF(Age>=10 And Age<=20,True,False)")

IIF(Age>=10 And Age<=20,True,False)

I want to perform function(iif) operation in a custom function.

Need this operation with function

 Dim MyTable As New DataTable
 MyTable.Columns.Add("Name")
 MyTable.Columns.Add("Age", GetType(Integer))
 MyTable.Rows.Add("Ali", 9)
 MyTable.Rows.Add("Amir", 15)
 MyTable.Rows.Add("Mansour", 18)
 MyTable.Columns.Add("isOk", GetType(Boolean), MyFunc(Age))  'Error

Function MyFunc(InAge) as Boolean
If InAge>=10 And InAge<=20 then
return true
else
Return False
End If
End Function
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,376 Reputation points Microsoft Vendor
    2024-07-29T02:58:41.5066667+00:00

    Hi @Mansour_Dalir ,

    Use a For Each loop to iterate through each row in the DataTable to call the MyFunc function with the Age column value to assigne the isOk column result for each row.

            For Each row As DataRow In MyTable.Rows
                row("isOk") = MyFunc(row("Age"))
            Next
    
        Function MyFunc(InAge As Integer) As Boolean
            If InAge >= 10 And InAge <= 20 Then
                Return True
            Else
                Return False
            End If
        End Function
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 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 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2024-07-28T06:39:37.56+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To use a function in an expression that adds a column to a DataTable, you can define the function using a lambda expression and then use it in the DataColumn constructor. Here's an example in VB:

    Dim workTable As New DataTable("Customers")
    workTable.Columns.Add("Total", GetType(Double))
    workTable.Columns.Add("SalesTax", GetType(Double), "Total * 0.086")
    Dim getDiscount = Function(price As Double) price * 0.1
    workTable.Columns.Add("Discount", GetType(Double), "getDiscount(UnitPrice)")
    

    In this example, we define a lambda expression getDiscount that takes a price parameter and returns price * 0.1. We then use this lambda expression in the DataColumn constructor for the "Discount" column, passing in "getDiscount(UnitPrice)" as the expression.


    References:


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.