要查询List中的某一个值,可以使用LINQ查询或者遍历List的方法。

  1. 使用LINQ查询:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int value = 3;
bool containsValue = list.Contains(value);
  1. 遍历List:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int value = 3;
bool containsValue = false;
foreach (int item in list)
{
    if (item == value)
    {
        containsValue = true;
        break;
    }
}

在上面的例子中,我们先创建了一个List并初始化了一些值。然后,我们定义了要查找的值value,并使用LINQ Contains方法或者遍历List来查找该值是否存在于List中。最后,我们将结果保存在一个布尔变量containsValue中。

C#查询List中的某一个值

原文地址: http://www.cveoy.top/t/topic/69D 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录