如何在VB.NET中使用多个FirstOrDefault变量选择Datatable

huangapple go评论76阅读模式
英文:

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

问题

我想要做datatable多选,但还没有成功。请指导。
<br>对于两个变量,CodeProductBarcode,我已经成功了,但要添加两个变量,ColorCodeSize,我还失败了。

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

 Private Sub process()
        Dim Barcode = TextBox1.Text.Trim()
        Dim CodeProduct = TextBox2.Text.Trim()
        Dim ColorCode = &quot;test&quot;
        Dim Size = &quot;test&quot;
        Dim row As DataRow = Nothing
        If dt.Rows.Cast(Of DataRow).Any() Then
            &#39;row = If(dt.Select($&quot;Barcode = &#39;{Barcode}&#39;&quot;).FirstOrDefault(), dt.Select($&quot;Codeproduct = &#39;{CodeProduct }&#39;&quot;).FirstOrDefault())
            row = dt.Select($&quot;Barcode = &#39;{Barcode}&#39;&quot; And $&quot;Codeproduct = &#39;{CodeProduct }&#39;&quot;).FirstOrDefault()
        End If
    End Sub
英文:

I want to do datatable select multiple but it doesn't work yet. Please guide.
<br>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)
        &#39;dt = New DataTable
        dt.Columns.AddRange({
        New DataColumn(&quot;No&quot;, GetType(Integer)),
        New DataColumn(&quot;CodeProduct&quot;, GetType(String)),
        New DataColumn(&quot;Barcode&quot;, GetType(String)),
        New DataColumn(&quot;ColorCode&quot;, GetType(String)),
        New DataColumn(&quot;Size&quot;, GetType(String)),
        New DataColumn(&quot;Qty&quot;, GetType(Integer))
    })
        Grid.DataSource = dt
    End Sub

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

答案1

得分: 0

根据此链接中的解决方案,尽管该解决方案是由@MichaelPetrotta用C#编写的,但对我来说提供了最佳解决方案。

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
英文:

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($&quot;Barcode = &#39;{Barcode}&#39; AND Codeproduct = &#39;{CodeProduct}&#39; AND ColorCode = &#39;{ColorCode}&#39; AND Size = &#39;{Size}&#39;&quot;).FirstOrDefault()
End If

huangapple
  • 本文由 发表于 2023年7月24日 19:56:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定