linq 按字符串列表构成的键查找行

4xrmg8kj  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(166)

如何使用String列表的键在DataGridView中查找行?我将单元格值与具有相同列的另一个网格进行比较...

Private key As New List(Of String)
Dim rowFromAnotherGrid As DataGridViewRow

...

Me.key = New List(Of String) From {"Key1", "Key2"}
Dim row As DataGridViewRow

'This is obviously wrong - Expression does not produce a value:
row = (From r In Me.grid.Rows
       Where Me.key.ForEach(Function(subkey)
         Return r.Cells(subkey).Value = rowFromAnotherGrid.Cells(subkey).Value
       End Function)).FirstOrDefault

差不多吧还是有更好的办法?

bweufnob

bweufnob1#

好吧,我是这样做的,还是相信有更好的解决办法:

'put searching key values in a list of string
Dim keyValuesToFind As List(Of String) = (From c As DataGridViewCell In rowFromAnotherGrid .Cells
                                                  From subkey In Me.key
                                                  Where c.OwningColumn.Name = subkey
                                                  Select CStr(c.Value)).ToList

...

Dim rowToFind As DataGridViewRow

'do the same for the grid in which we're searching and compare the lists
    rowToFind = Me.grid.Rows.Where(Function(r)
                                       Dim rowKey = (From c As DataGridViewCell In r.Cells
                                                     From subkey In Me.key
                                                     Where c.ColumnInfo.Name = subkey
                                                     Select CStr(c.Value)).ToList
                                       Dim ret = rowKey.Except(keyValuesToFind).Count = 0
                                       Return ret
                                   End Function).FirstOrDefault

相关问题