Learn how to use Count method in LINQ using C#, you can get the count of entity in any collection, there is a built-in method to get count in LINQ query syntax.
int totoal= listObject.Count();
LINQ Count is Enumerable extension method, it comes under System.Linq
namespace. We often use count method to get the total number of items in any collection object.
IEnumerable<string> countries = new List<string> { "India", "USA", "UK", "Canada", "Australia" }; int count = countries.Count();
Count with Predicate example
Count the total number of items in any specified predicate
IEnumerable<int> items = new List<int> { 10, 20, 30, 40, 50, 100 }; int count = items.Where(x => x < 49).Count();
Count with Query Syntax
IEnumerable<int> items = new List<int> { 10, 20, 30, 40, 50, 100 }; int count = (from n in items select n).Count();
Count method will be used as last method call in the linq query, there should not be any other method after that count call.