Linq By Example

Count

Returns the count of items that match a predicate.

Collection Count vs LINQ Count

Count exists as a property on all collections, and is not part of System.Linq. For example
    
using System;
using System.Collections.Generic;
using System.Linq;

var myList = new List { 1, 2, 3, 4, 5 };
var myCollectionCount = myList.Count;
var myLinqCount = myList.Count();
Console.WriteLine(myCollectionCount);
Console.WriteLine(myLinqCount);

  

Count With Conditions

You can pass a predicate to count to return the number of items that match a condition.
    
using System;
using System.Collections.Generic;
using System.Linq;

var myList = new List { 1, 2, 3, 4, 5 };
var myCount = myList.Count(x => x >= 3);
Console.WriteLine(myCount);

  

Performance Of Count vs Any

When determining if there are any items in the list matching a condition, you should instead use the Any LINQ query. This is because the Any query will immediately exit upon finding any item matching a condition, whereas Count will continue until the end of the collection.
    
using System;
using System.Diagnostics;
using System.Linq;

var myList = Enumerable.Range(1, 10000000);

var stopWatchAny = new Stopwatch();
stopWatchAny.Start();
var resultAny = myList.Any(x => x == 1);
stopWatchAny.Stop();
Console.WriteLine($"Any completed in : {stopWatchAny.ElapsedMilliseconds} ms");

var stopWatchCount = new Stopwatch();
stopWatchCount.Start();
var resultCount = myList.Count(x => x == 1) > 0;
stopWatchCount.Stop();
Console.WriteLine($"Count completed in : { stopWatchCount.ElapsedMilliseconds} ms");