Simple.Data

Simple.Data defines a number of commands for retrieving data from a data store. These can then be daisychained in a LINQ-like fashion with further methods to modify the basic query.

GetCountBy

GetCountBy returns an integer count of records in a table that match a given at least one column name and value to filter by.

There are two ways to form a GetCountBy method.

Syntax

public int GetCountBy(
		Object[] filterExpressions
	)

public int GetCountBycolumnName1[AndcolumnName2…](
	Object columnValue1
	[, Object columnValue2]
)

Parameters

filterExpressions
Type: object[]
A set of named parameters where the parameter name is that of column being filtered against and the parameter value is that which the column should equal.
columnName1[, columnName2, …, columnNameX]
Type: string The name of the column(s) being filtered against
columnValue1[, columnValue2, …, columnValueX]
Type: object (typically string or int) columnValueY contains the value with which column with name columnNameY should equal.

Return Value

Type: int
A number that represents how many records in the table satisfy the given conditions.

Exceptions

Exception Condition
ArgumentException The number of columnValues does not match the number of columnNames or no arguments at all have been passed to the method.
FormatException The type of a columnValue does not match the actual type of the corresponding columnName

Remarks

GetCountBy is used to return all the data from a table based on a set of criteria where the value of columns in a row

If you wish to include a column name in a filter that contains the string “and” in it, you must use the named parameter form of GetCountBy. If you have a column called, for example, HandlingCode, and you try to call

GetCountByHandlingCode(1);

the parser will split that into two columns, H & lingCode, and throw an ArgumentException. So you have to use the named parameter form for columns with “and” in the name. For example,

GetCountBy(HandlingCode:1);

Examples

One Criteria

The following example retrieves the number of records in the Albums table with GenreId equal to 1.

int albums = Database.Open().Albums.GetCountByGenreId(1);
	
// The named parameter equivalent call to GetCountBy() is
// int albums = Database.Open().Albums.GetCountBy(GenreId:1);

Simple.Data sends the following SQL to the database when albumCount is evaluated.

select COUNT(*) from [dbo].[Albums] 
WHERE 
   [dbo].[Albums].[GenreId] = @p1
@p1 (Int32) = 1

Two Criteria

The following example retrieves the number of records in the Albums table with GenreId equal to 1 and ArtistId equal to 120.

int albums = Database.Open().Albums.GetCountByGenreIdAndArtistId(1,120);
	
// The named parameter equivalent call to GetCountBy() is
// int albums = Database.Open().Albums.GetCountBy(GenreId:1,ArtistId:120);

Simple.Data sends the following SQL to the database when album is evaluated.

select COUNT(*) from [dbo].[Albums] 
WHERE 
    ([dbo].[Albums].[GenreId] = @p1 AND [dbo].[Albums].[ArtistId] = @p2)
@p1 (Int32) = 1
@p2 (Int32) = 120