简体   繁体   English

计算集合中的项目数

[英]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". 在“已读”属性中,存在等于“ 1”或“ 0”的值。 How to count every Readen that is equal to "1" ? 如何计算每个等于“ 1”的Readen?

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 MessageBox显示零

Use Linq , or to be more precise, the Enumerable.Count method: 使用Linq ,或更确切地说,使用Enumerable.Count方法:

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... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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