0

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

Clemens
  • 123,504
  • 12
  • 155
  • 268
keno
  • 93
  • 3
  • 11

2 Answers2

3

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

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

var count = items.Count(c => c.Readen == 1);
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I don't get IEnumerable.. what comes behind "items=" ? :/ (Just started with wpf) – keno Jan 02 '13 at 13:13
  • This isn't WPF, just plain .Net Framework. [IEnumerable](http://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx) is the base interface of all collection types in .Net, and i guess you're going to organize you `Conversation` objects in some kind of collection. – Clemens Jan 02 '13 at 13:25
  • Yes, my Conversation is binded by an ObservationCollection to show into a datagrid – keno Jan 02 '13 at 13:42
  • Then items is your ObservableCollection – paparazzo Jan 02 '13 at 14:09
0

Some thing like this...

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

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

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
  • tried this before, but does not work (cs.Readen is Null). Don't I have to do a loop or something to go into al the items ? – keno Jan 02 '13 at 13:12
  • Have u assigned properly., Check the assigning place whether you are assigning properly or not... – RajeshKdev Jan 02 '13 at 13:17
  • yes, after Conversation is filled, I call this in a method. (See my post) – keno Jan 02 '13 at 13:52
  • See @keno you are trying like this.. In `Conversation cs = new Conversation();` Here you are creating instance right.Then In Next line you have to assign.But here you are trying to get a value without assign anything. If you are creating instance for a class you have to assign some where then only values will come. Inside some method assign the value for `cs.Readen = 1;` somthing like this. Then check the `If(cs.Readen.Equals(1)) { //Your Statements Here... }`.It will work. – RajeshKdev Jan 02 '13 at 14:24