LINQ Count Method Example

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();
Count in LINQ C#

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 collection count example
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.

 
Linq count method c# example: count in linq query syntax
LINQ (language integrated query) allow you to write query on database objects like ado.net, entity framework etc, LINQ is type safe, easy to convert database object to list objects and business objects in secure and scalable way.
linq Interview Questions Answers
LINQ C# examples | Join Asp.net MVC Course