C# 数组对象状态判断:判断所有对象 status 是否都为 1
您可以使用 LINQ 库中的 All 方法来判断数组中所有对象中的 status 是否都为 1。示例如下:
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
// 示例数组
var array = new[]
{
new { status = 1 },
new { status = 1 },
new { status = 1 }
};
// 判断数组中所有对象中的 status 是否都为 1
bool allStatusIsOne = array.All(obj => obj.status == 1);
Console.WriteLine(allStatusIsOne); // 输出: True
}
}
在上述示例中,我们使用了一个匿名类型的数组作为示例数据,并使用 All 方法对数组中的每个对象进行判断,判断条件是对象的 status 属性是否等于 1。如果数组中所有对象的 status 都为 1,则返回 True,否则返回 False。在示例中,由于数组中所有对象的 status 都为 1,因此输出结果为 True。
原文地址: https://www.cveoy.top/t/topic/i17e 著作权归作者所有。请勿转载和采集!