Count() a specfic attribute within a list c#

前端 未结 3 1076
一向
一向 2021-01-28 17:32
if (gardenvlist.Count() == getava.Count())
{

}
else if(oceanvlist.Count() == getava.Count())
{

}
else if (cityvlist.Count() == getava.Count())
{

}

<

3条回答
  •  渐次进展
    2021-01-28 18:08

    Use the overload which takes a predicate. Here is the signature:

    public static int Count(this IEnumerable source, 
        Func predicate);
    

    Imaging you have this:

    public class Room
    {
        public string Name { get; set; }
    }
    

    Then you put them into a list:

    var rooms = new List()
    {
        new Room { Name = "Living" },
        new Room { Name = "Kitchen" }
    };
    

    You want to know how many rooms have the name "Living":

    int count = rooms.Count(x => x.Name == "Living");
    

提交回复
热议问题