英文:
How to Datatable select with multiple FirstOrDefault variables in VB.NET
问题
我想要做datatable多选,但还没有成功。请指导。
<br>对于两个变量,CodeProduct
和Barcode
,我已经成功了,但要添加两个变量,ColorCode
和Size
,我还失败了。
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
英文:
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)
'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
答案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($"Barcode = '{Barcode}' AND Codeproduct = '{CodeProduct}' AND ColorCode = '{ColorCode}' AND Size = '{Size}'").FirstOrDefault()
End If
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论