0

i have this class:

   class Article
    {
            public int Id { get; set; }
            public string Name { get; set; }
    }


    List<Article> arts = new List<Article>();

I have a list of objects. These objects have two variables ID, Name. I need to check if in this list there is an article with name = "Cheese". How can I do to make this search returns a Boolean value? Any help would be great! Thanks!

4

2 回答 2

4
bool hasCheese = arts.Any(a => a.Name == "Cheese");
于 2013-06-27T09:34:40.580 回答
2

简单的: -

bool contains = arts.Any(x => x.Name == "Cheese");

Any将返回一个布尔值,指示列表是否包含Cheese

于 2013-06-27T09:35:10.723 回答