Count() a specfic attribute within a list c#

前端 未结 3 1074
一向
一向 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:13

    There is another overload of Count() that takes a lambda expression as a parameter where you can filter elements to count from the source collection.

    Say you have a list of strings like this:

    var list = new List() {
       "a",
       "b",
       "c",
       "aa",
       "bbb"
    };
    

    Then you can use it like this:

    var countSingles = list.Count(str => str.Length == 1);
    

    This will count the elements of the list with length 1, so here it would return 3.

提交回复
热议问题