Hello my friends, In this tutorial, we are going to understand Count operator in Linq. Count operator is similar to count() in SQL server which is used to return the number of elements in a collection or the number of elements that have satisfied a given condition.

Count operator in Linq:

Count operator is used for returning the number of elements in a collection or the number of elements that have satisfied a given condition.

Count method overloads:

  1. int Count<TSource>();
  2. int Count<TSource>(Func<TSource, bool> predicate);

The first method return the number of elements in a collection.

The second method returns the number of elements in the collection for the specified condition given as lambda expression.

Example: using the Count method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

class Program
{
 
static void Main(string[] args)
{

IEnumerable<int> items = new List<int> {10,23, 33,44,32,22 };

int count = items.Count();
Console.WriteLine("Total number of elements {0}", count);

Console.ReadLine(); 
}
 
}

Example: Count elements using the query expression-

The output of this example is similar to the above example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

class Program
{
 
static void Main(string[] args)
{

IEnumerable<int> items = new List<int> {10,23, 33,44,32,22 };

int count = (from c in items select c).Count();
Console.WriteLine("Total number of elements {0}", count);

Console.ReadLine(); 
}
 
}

Example: Using Predicate-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

class Program
{
 
static void Main(string[] args)
{

IEnumerable<int> items = new List<int> {10,23, 33,44,32,22 };

int count = items.Count(x => x%2==0);
Console.WriteLine("Total number of even elements {0}", count);

Console.ReadLine(); 
}
 
}

Example: Count the number of employees having a salary greater than 30000-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

public class Employee
{
public string EmployeeCode { get; set; }
public string EmployeeName { get; set; }
public string Gender { get; set; }
public string Designation { get; set; }
public double Salary { get; set; }
}

class Program
{
public static List<Employee> DataSource()
{
List<Employee> lstEmployee = new List<Employee>();
Employee objEmployee1 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee2 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee3 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 };
Employee objEmployee4 = new Employee() { EmployeeCode = "E004", EmployeeName = "Nidhi Verma", Gender = "Female", Designation = "HR", Salary = 40000 };
Employee objEmployee5 = new Employee() { EmployeeCode = "E005", EmployeeName = "Ayushman Lakhotia", Gender = "Male", Designation = "Project Manager", Salary = 80000 };
Employee objEmployee6 = new Employee() { EmployeeCode = "E006", EmployeeName = "Neha Sharma", Gender = "Female", Designation = "Graphic Designer", Salary = 25000 };
lstEmployee.Add(objEmployee1);
lstEmployee.Add(objEmployee2);
lstEmployee.Add(objEmployee3);
lstEmployee.Add(objEmployee4);
lstEmployee.Add(objEmployee5);
lstEmployee.Add(objEmployee6);
return lstEmployee;
}
static void Main(string[] args)
{


int count = DataSource().Count(x => x.Salary>30000);
//the below queries produces the same output
//int count1=(from c in DataSource() where c.Salary>30000).Count();
//int count2 = DataSource().Where(e => e.Salary > 30000).Count();
Console.WriteLine("Total Employees having Salary Greater than 30000 {0}", count);
 
Console.ReadLine(); 
}
 
}

View More:

Conclusion:

I hope you would love this post. Please feel free to comment for any technical help. Your feedback and suggestion are always welcome to me.

Thank You.

Leave a Comment