本文介绍了检查列表属性中是否已经有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我尝试查看列表中是否已包含该文本.代码总是放在if语句中.为什么?

Below I am trying to see if the text is already contained in the list. The code always goes into the if statement.Why?

if(MyGlobals.ListOfItemsToControl.FindAll(x => x.sItemName == info.FullName ) != null)
{
   ...
}

推荐答案

使用Any代替FindAll

use Any instead of FindAll

if(MyGlobals.ListOfItemsToControl.Any(x => x.sItemName == info.FullName ))

如果找不到值,

FindAll不会返回null,它会返回一个空集合.

FindAll doesn't return null if value is not found, it returns an empty collection.

所以您可以(但不能)

 if(MyGlobals.ListOfItemsToControl.FindAll(x => x.sItemName == info.FullName ).Any())

这篇关于检查列表属性中是否已经有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:41
查看更多