0

I want to count the occurences of objects within a List<T> that match a certain condition.
For example like this
int List<T>.Count(Predicate<T> match)
So for example if have a list of chores, I can see how many are overdue.

int overdue = listOfChores.Count((element) => { return element.DueDate <= DateTime.Today; });  

I know that does not exist and so far I solve problems like that in the following way:

int overdue = listOfChores.FindAll([...]).Count;

However that allocates and initializes a new List etc. only to get the count.

A way to do this with less allocation overhead etc.:

int good = 0;
foreach(chore element in listOfChores)
    if(element.DueDate <= DateTime.Today)
        good++;

The last approach can also be exandend to count several conditions without iterating over the loop more than once. (I already found that getting the count property only takes O(1), but making the List to count from still eats a lot of time)

int a = 0;
int b = 0;
foreach(chore element in listOfChores)
    if(element.CondA)
        a++;
    if(element.CondB)
        b++;

Given this I could even imagine something like
int[] List<T>.Count(Predicate<T>[] matches)

My question(s):
Is there such a thing, just I haven't found it yet?
If not: What would be way to implement such functionality?

EDIT :
Adding LINQ looks like it fixes it.

Community
  • 1
  • 1
Mark
  • 419
  • 1
  • 6
  • 21
  • 1
    Which version of c# ? there is [Enumerable.Count Method (IEnumerable, Func)](http://msdn.microsoft.com/fr-fr/library/bb535181(v=vs.110).aspx) – NicoD Jul 08 '14 at 12:53
  • 1
    Have a look at the LINQ method Count: http://msdn.microsoft.com/en-us/library/vstudio/system.linq.enumerable.count(v=vs.100).aspx – Rico Suter Jul 08 '14 at 12:55
  • 1
    possible repeat of http://stackoverflow.com/questions/3853010/get-item-count-of-a-list-using-linq – Ashish Rajput Jul 08 '14 at 12:56
  • @NicoD I was on .Net 4.0. I'll give that link a shot right now. – Mark Jul 08 '14 at 12:59
  • @AshishRajput good link. – Mark Jul 08 '14 at 13:02
  • 1
    Without System.Linq you can't use the extension methods like Enumerable.Count that's why you haven't find it yet – NicoD Jul 08 '14 at 13:11
  • @NicoD I noticed. If you'd type up your helpful advice as a full answer, I could mark it as such ;-) – Mark Jul 08 '14 at 13:15

2 Answers2

9

You just have your syntax slightly off. This is how to use Count :

int overdue = listOfChores.Count(element => element.DueDate <= DateTime.Today);

If you already have a Predicate<T> and want to pass it to Count just call it like a function:

Predicate<Chore> p = (element) => element.DueDate <= DateTime.Today; 

int overdue = listOfChores.Count(element => p(element));
D Stanley
  • 149,601
  • 11
  • 178
  • 240
3

There's is a count method using a predicate : see Enumerable.Count Method (IEnumerable, Func)

Note that this method is an extension method and you can use it only if you add a reference to the System.Linq namespace.

NicoD
  • 1,179
  • 8
  • 19