检查列表中的对象是否具有特定的字段内容

时间:2014-02-06 10:12:12

标签: c# list

对不起问题标题,我不知道如何用一句话来解释它......

我有一个这样的课程

public class xyz
{
    public static string attr1;
    public static string attr2;
    public static string attr3;
}

如何检查是否存在attr1 ==&#34; aaa&#34;在List<xyz>

有类似

的东西
List<xyz> MyList = new List<xyz>();

[...]

bool attr1_exist = MyList.attr1.Contains("aaa");

2 个答案:

答案 0 :(得分:4)

这应该这样做:

bool attr1_exist = MyList.Exists(s=> s.attr1 == "aaa")

答案 1 :(得分:1)

使用Any()

bool attr1_exist = MyList.Any(x => x.attr1.Contains("aaa"));
相关问题