C# LINQ Queryable.Count Method in ASP.Net

Updated on 13 Apr 2012,
Published on 21 Jan 2011

The Count method of System.Linq.Queryable class enables to get the number of elements stored in a data source sequence. There are two overloaded functions of Count method that enable you to get the number of all the elements stored in a sequence or the number of elements which satisfy the specified condition. Following are the overloaded functions of LINQ Count Methods that accept zero or one parameter and return the number of elements accordingly:

1.

public static int Count<TSource>(this IQueryable<TSource> source);

It takes no parameter and returns the total number of elements in a sequence.

2.

public static int Count<TSource>(this IQueryable<TSource> source, Func<TSource, bool> predicate);

It takes one parameter as test condition and returns the number of elements satisfying that condition.

C# LINQ Count Method Examples in ASP.Net

// Example 1:
List<Employee> Employees = new List<Employee>()
{
    new Employee(1, "Terry", "Adams", 30, 5),
    new Employee(2, "Hugo", "Garcia", 27, 3),
    new Employee(3, "Fadi", "Fakhouri", 32, 5),
    new Employee(4, "Debra", "Garcia", 30, 4),
    new Employee(5, "Lance", "Tucker", 35, 7),
    new Employee(6, "Hanying", "Feng", 35, 5),
    new Employee(7, "Michael", "Tucker", 28, 5),
    new Employee(8, "Eugene", "Zabokritski", 40, 10),
    new Employee(9, "Sven", "Mortensen", 37, 8),
    new Employee(10, "Svetlana", "Omelchenko", 36, 7)
};

// implementing Count method
int countAll = Employees.AsQueryable().Count();
Response.Output.Write("Total Employees: {0}<br />", countAll);

// Example 2:
// implementing overloaded function of Count method
int countConditionally = Employees.AsQueryable().Count(employee => employee.Experience >= 5);
Response.Output.Write("Employees with minimum experience of 5 years: {0}<br />", countConditionally);

// Output:
// Total Employees: 10
// Employees with minimum experience of 5 years: 8

C# Employee Class:

public class Employee
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public int Experience { get; set; }

    public Employee(int Id, string firstName, string lastName, int age, int experience)
    {
        this.ID = Id;
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;
        this.Experience = experience;
    }
}

In the above C# sample code there are two examples of Count Method. In the first example we have passed no parameter to the LINQ Count Method that will return the total number of items stored in the generic list collection of employee class objects. Whereas in the second example we have specified a test condition as Standard query operation to get the number of employees having experience of at least 5 years.

Continue to the next tutorial: C# LINQ Queryable.DefaultIfEmpty Method in ASP.Net to learn how to handle and return a default value if a sequence is empty.

0 Responses to "C# LINQ Queryable.Count Method in ASP.Net"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers