我有一个第三方网格,我正在使用 LINQ 检查其上的所有行 (GetRows()
) - 检查特定单元格 (_ObjectKey
) 不包含值。我想为此使用 LINQ Any
方法。
但我的语句总是返回 false
,即使 sentGrid
确实 包含该值。有明显的错误吗?
if (sentGrid.GetRows().Any(r => r.Cells[_ObjectKey].Value == theValue) == 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);
}
关于c# - LINQ Any - 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267301/