我对 LINQ 还比较陌生,并且遇到的麻烦比任何事情都多,但我真的很喜欢到目前为止所看到的。因此,考虑到这一点,我有一个 VB.NET 搜索例程,下面提供了其中的一部分,它检查 DataGridView 中的所有文本单元格是否有给定的字符串(包括),使用一组基本的嵌套循环来执行搜索:
' Search for the first occurrence of the given string
For Each row As DataGridViewRow In dgvMembers.Rows
' Skip the new row
If row.IsNewRow Then Exit For
' Loop through all the cells in the current row
For Each cell As DataGridViewCell In row.Cells
' Skip non-text cells
If cell.GetType IsNot GetType(DataGridViewTextBoxCell) Then Continue For
' Search for our matching text
If cell.Value.ToString.ToUpper.Contains(searchText) Then
' Select the cell if we have a match
dgvMembers.CurrentCell = cell
WriteMessage("String '{0}' found.", searchText)
Exit Sub
End If
Next
Next
' If we get to this point, we didn't find anything
WriteMessage("String '{0}' NOT found.", searchText)
非常简单。现在,我的问题是:有没有办法使用 LINQ 复制此行为?基本上我希望查询选择(或返回)第一个文本包含搜索字符串的 DataGridViewCell。我已经对子查询等进行了一些修改,但我仍然无法理解这些概念(我猜是写 T-SQL 太多年了)。
显然,嵌套循环工作得很好,所以这确实是一种好奇。提前致谢!
最佳答案
我能够使用此代码并取得一些成功:
Dim qry = From theRow as DataGridViewRow In dgvMembers.Rows, _
theCell as DataGridViewCell In theRow.Cells _
Where theCell.Value.ToString.ToUpper = searchText _
Select theCell
Dim matchCell as DataGridViewCell = qry.First
dgvMembers.CurrentCell = matchCell
...等等...
关于vb.net - 使用 LINQ 查询的 DataGridView 单元格搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3321116/