使用LINQ查询进行DataGridView单元格搜索

Mik*_*oux 2 linq vb.net datagridview subquery

我对LINQ来说还是比较陌生的,并且比任何事情都做得更磕磕绊绊,但我真的很喜欢到目前为止看到的内容.因此,考虑到这一点,我有一个VB.NET搜索例程,下面提供了一部分,它检查DataGridView中给定字符串(包括)的所有Text单元格,使用一组基本的嵌套循环来执行搜索:

' 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)
Run Code Online (Sandbox Code Playgroud)

非常直截了当.现在,我的问题是:有没有办法使用LINQ复制这种行为?基本上我想查询选择(或返回)其文本包含搜索字符串的第一个DataGridViewCell.我已经对子查询等进行了一些修改,但是我仍然无法绕过这些概念(我猜太多年来编写T-SQL).

显然嵌套循环工作正常,所以这更像是一种好奇心.提前致谢!

kns*_*lyr 5

我能够使用此代码取得一些成功:

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
Run Code Online (Sandbox Code Playgroud)

......等......