vb.netselectdatatabledatasetextensions

How to Datatable select with multiple FirstOrDefault variables in VB.NET


I want to do datatable select multiple but it doesn't work yet. Please guide.
For 2 variables, CodeProduct and Barcode, I've succeeded but to add 2 variables, ColorCode and Size, I still fail

Public dt As New DataTable()
    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)
        'dt = New DataTable
        dt.Columns.AddRange({
        New DataColumn("No", GetType(Integer)),
        New DataColumn("CodeProduct", GetType(String)),
        New DataColumn("Barcode", GetType(String)),
        New DataColumn("ColorCode", GetType(String)),
        New DataColumn("Size", GetType(String)),
        New DataColumn("Qty", GetType(Integer))
    })
        Grid.DataSource = dt
    End Sub

 Private Sub process()
        Dim Barcode = TextBox1.Text.Trim()
        Dim CodeProduct = TextBox2.Text.Trim()
        Dim ColorCode = "test"
        Dim Size = "test"
        Dim row As DataRow = Nothing
        If dt.Rows.Cast(Of DataRow).Any() Then
            'row = If(dt.Select($"Barcode = '{Barcode}'").FirstOrDefault(), dt.Select($"Codeproduct = '{CodeProduct }'").FirstOrDefault())
            row = dt.Select($"Barcode = '{Barcode}'" And $"Codeproduct = '{CodeProduct }'").FirstOrDefault()
        End If
    End Sub

Solution

  • as per solution from Here's a link! and although the solution is made in C# by @MichaelPetrotta but provide the best solution for me

    If dt.Rows.Cast(Of DataRow).Any() Then
    row = dt.Select($"Barcode = '{Barcode}' AND Codeproduct = '{CodeProduct}' AND ColorCode = '{ColorCode}' AND Size = '{Size}'").FirstOrDefault()
    End If