Back to: LINQ Tutorial For Beginners and Professionals
LINQ Count Method in C# with Examples
In this article, I am going to discuss the LINQ Count Method in C# with Examples. Please read our previous article where we discussed the LINQ Average Method in C# with Examples. As part of this article, we are going to discuss the following pointers.
- What is LINQ Count Method in C#?
- Multiple Examples using both Method and Query syntax.
What is LINQ Count Method in C#?
The LINQ Count() Method belongs to the category of Aggregate Operators. The LINQ Count Method is used to return the number of elements present in the collection or the number of elements that have satisfied a given condition. There are two overloaded versions of the LINQ Count() Extension method available. They are as follows.
As you can see in the above image, the first overloaded version does not take any parameter and it simply returns the count of the number of elements present in the collections. On the other hand, the second overloaded version takes one predicate as a parameter and then it returns the count of the number of elements that have satisfied the given condition which we can specify using a lambda expression or by using a predicate function. As you can see in the above image, both method’s return type is int. So, it is always going to return an integer number which indicates the number of elements present in the collection or in the data source.
Example to Understand LINQ Count Method in C#:
Let us understand the Count() method in C# with Examples. The following example returns the number of elements present in the collection using both Query and Method Syntax. Unlike the Sum, Average, Min, and Max Aggregate functions, we can use Count Method with any data type. In the below example, we are using integer collection. We don’t have any operator called count in the LINQ query syntax, so, we need to use mixed syntax.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] intNumbers = new int[] { 60, 80, 50, 90, 10, 30, 70, 40, 20, 100 }; //Using Method Syntax int MSCount = intNumbers.Count(); //Using Query Syntax var QSCount = (from num in intNumbers select num).Count(); Console.WriteLine("No of Elements = " + MSCount); Console.ReadKey(); } } }
Output: No of Elements = 10
Example to Understand Count Method with Where Extension Method in C#
Let us see an example to Understand how we can use the LINQ Count Method along with the LINQ Where Extension Method using both Method and Query Syntax. Now our requirement is to return the number of elements present in the collection which are greater than 40. The following example code exactly does the same.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] intNumbers = new int[] { 60, 80, 50, 90, 10, 30, 70, 40, 20, 100 }; //Using Method Syntax int MSCount = intNumbers.Where(num => num > 40).Count(); //Using Query Syntax var QSCount = (from num in intNumbers where num > 40 select num).Count(); Console.WriteLine("No of Elements = " + MSCount); Console.ReadKey(); } } }
Output: No of Elements = 6
Example to Understand How to use LINQ Count Method with Predicate in C#
Let us see an example to Understand How to use LINQ Count Method with Predicate in C# using both Method and Query Syntax. Instead of using the Where Extension method to filter the data, we can also use the other overloaded version of the Count method which takes a Predicate as a parameter, and within that predicate, we can write the logic to filter the data. In the below example, within the Count Method, we are using a Predicate and we are providing the condition whether the number is greater than 40 or not. If the number is greater than 40, then we are returning true else we are returning false. The following example will return the number of elements present in the collection which are greater than 40.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] intNumbers = new int[] { 60, 80, 50, 90, 10, 30, 70, 40, 20, 100 }; //Using Method Syntax with a Predicate int MSCount = intNumbers.Count(num => { if (num > 40) return true; else return false; }); //Using Query Syntax with a Predicate int QSCount = (from num in intNumbers select num).Count(num => { if (num > 40) return true; else return false; }); Console.WriteLine("No of Elements = " + MSCount); Console.ReadKey(); } } }
Output: No of Elements = 6
Example to Understand LINQ Count Method with Complex Type in C#:
Let us see an example to Understand How to use Count Method with Complex Type in C#. We are going to work with the following Employee class. As you can see, the following Employee class contains four properties i.e. ID, Name, Salary, and Department as well as contains one method i.e. GetAllEmployees() which will return the list of all the employees and this is going to be our data source.
using System.Collections.Generic; namespace LINQDemo { public class Employee { public int ID { get; set; } public string Name { get; set; } public int Salary { get; set; } public string Department { get; set; } public static List<Employee> GetAllEmployees() { List<Employee> listStudents = new List<Employee>() { new Employee{ID= 101,Name = "Preety", Salary = 10000, Department = "IT"}, new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"}, new Employee{ID= 103,Name = "James", Salary = 50000, Department = "Sales"}, new Employee{ID= 104,Name = "Hina", Salary = 20000, Department = "IT"}, new Employee{ID= 105,Name = "Anurag", Salary = 30000, Department = "IT"}, new Employee{ID= 106,Name = "Sara", Salary = 25000, Department = "IT"}, new Employee{ID= 107,Name = "Pranaya", Salary = 35000, Department = "IT"}, new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"}, new Employee{ID= 109,Name = "Sam", Salary = 45000, Department = "Sales"}, new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"} }; return listStudents; } } }
Now, our requirement is to calculate the Number of Employees present in the collection. The following example returns the Number of Employees present in the collection using the LINQ Count Method.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { //Using Method Syntax var MSCount = Employee.GetAllEmployees().Count(); //Using Query Syntax var QSCount = (from emp in Employee.GetAllEmployees() select emp).Count(); Console.WriteLine("Total No of Employees = " + QSCount); Console.ReadKey(); } } }
Output: Total No of Employees = 10
Example using Count and Where Extension Method in C#:
Let us see an example to Understand How to use both Count and Where Extension Methods with Complex Type using both Method and Query Syntax. Our requirement is to return the number of employees who belong to the IT department. The following example exactly does the same. Using the Where Extension Method we are filtering the IT department employees and then calling the Count method which will return the number of employees present in the IT department.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { //Using Method Syntax var MSCount = Employee.GetAllEmployees() .Where(emp => emp.Department == "IT") .Count(); //Using Query Syntax var QSCount = (from emp in Employee.GetAllEmployees() where emp.Department == "IT" select emp).Count(); Console.WriteLine("Total No of Employees of IT Department = " + QSCount); Console.ReadKey(); } } }
Output: Total No of Employees of IT Department = 5
Let’s rewrite the previous example using the custom predicate. As you can see, within the Count method, we are checking the Department property value and if the Department is It, we are returning true else we are returning false.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { //Using Method Syntax var MSCount = Employee.GetAllEmployees() .Count(emp => { if (emp.Department == "IT") return true; else return false; }); //Using Query Syntax var QSCount = (from emp in Employee.GetAllEmployees() select emp).Count(emp => { if (emp.Department == "IT") return true; else return false; }); Console.WriteLine("Total No of Employees of IT Department = " + QSCount); Console.ReadKey(); } } }
Output: Total No of Employees of IT Department = 5
In the next article, I am going to discuss the LINQ Aggregate Method in C# with Examples. I hope you understood the use and need of the LINQ Count Method in C# with Examples. I hope you enjoy this LINQ Count Method in C# with Examples article.