LINQ任意-总是返回false

本文关键字:返回 false 任意 LINQ | 更新日期: 2023-09-27 18:09:04

我有一个第三方网格,我使用LINQ来检查它(GetRows())上的所有行-检查特定单元格(_ObjectKey)是否包含值。我想使用LINQ Any方法。

但是我的语句总是返回false,即使sentGrid 确实包含值。有没有明显的错误?

if (sentGrid.GetRows().Any(r => r.Cells[_ObjectKey].Value == theValue) == false)

LINQ任意-总是返回false

这个问题是由于使用==而不是. equals比较两个对象类型。

最终代码:

// Add items to target grid if they're not already there
if(!sentGrid.GetRows().Any(r => r.Cells[_ObjectKey].Value.Equals(theValue)))
{
   sentGrid.AddItem(theValue);
}