简体   繁体   中英

Count Number of items in a collection

How to count items that are equal to a certain value and place it in a label ?

class Conversation
{
    public string Id { get; set; }
    public int Readen { get; set; }
    public string Recipient { get; set; }
}

In Readen property, there are values that are equal to "1" or to "0". How to count every Readen that is equal to "1" ?

Update;

tried this calling after Conversation is filled:

        private void CountUnread() {


        int i = 0;

       Conversation cs = new Conversation();

       if (cs.Readen == "1") {
           i++;
       }
       MessageBox.Show(i.ToString());

    }

MessageBox shows zero

Use Linq , or to be more precise, the Enumerable.Count method:

IEnumerable<Conversation> items = ...
...

var count = items.Count(c => c.Readen == 1);

Some thing like this...

Conversation cs = new Conversation(); //If you are in another class create instance

if (cs.Readen.Equals(1)) { //Your Statements Here... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM