Counting Record

Syntax

int Count(filter, maxRows, orderBy);

Arguments

filter

Expression<Func<T, bool>>

Criteria by which the records of the repository should be filtered.

This parameter must not be empty.

null

maxRows

int?

Maximal number of records that should be filtered.

This parameter is optional.

null

orderBy

Func<IQueryable<T>, IOrderedQueryable<T>>

Order in which the filtered records should be stored.

This parameter is optional.

null

Description

Counts the number of all records from a repository that fulfill the criteria specified in the parameter filter.

If rows is not 0, it makes sure that the number of filtered records does not exceed the specified number, i.e. it will stop counting after the limit is reached (even if there are more records in the repository that fulfill the criteria) and the resulting number will be the maximum limit.

If orderBy is not null, it first orders the records according to the attributes specified in the parameter orderBy before counting the records from the newly ordered list.

Examples

Example 1: Counting records by only one criterion

The following program illustrates the function call with only one criterion:

var count = repo.Count(x => x.ProductID == "ADAM-800-BTO");

Function will count all the records from the repository that have "ADAM-567-BTO" as Production ID and store the resulting number into the variable "count".

Example 2: Filtering records by multiple criteria

The following program illustrates the function call with multiple criteria:

var count = repo.Count(x => x.ProductID == "ADAM-800-BTO"
                         && x.Quantity > 10 
                         && x.IsUrgent == true);

Function will count all the records from the repository that have "ADAM-567-BTO" as Production ID, have a Quantity of more than 10 and are Urgent, store the resulting number into the variable "count".

Example 3: Filtering records by multiple criteria with maximum limit:

The following program illustrates the function call with multiple criteria and specified maximum limit:

var count = repo.Count(x => x.ProductID == "ADAM-800-BTO"
                         && x.Quantity > 10,
                       100);

Function will count a maximum of 100 records from the repository that have "ADAM-567-BTO" as Production ID and a Quantity of more than 10, and store the resulting number into the variable "countExample3". If more than 100 records fulfill the criteria, function will stop counting after the 100th record and store "100" into the variable "count".

Example 4: Filtering records by multiple criteria with maximum limit + order:

The following program illustrates the function call with multiple criteria, specified maximum limit and order:

var countExample4 = repo.Get(x => x.ProductID == "ADAM-567-BTO" 
                               && c.Quantity > 10, 
                             100,
                             o => o.OrderBy(c => c.Timestamp);                            );

Function will order the records by their Timestamp attribute, count a maximum of 100 records from the ordered repository that have "ADAM-567-BTO" as Production ID and a Quantity of more than 10, and store the resulting number into the variable "orderList". If more than 100 records fulfill the criteria, function will stop counting after the 100th record and store "100" into the variable "count".

Last updated