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.

GetCount

GetCount returns an integer count of records in a table that match a given SimpleExpression.

Syntax

public int GetCount(
		SimpleExpression criteria
	)

Parameters

criteria
Type: SimpleExpression
A (concatenated sequence of) search criteria. For example, dbo.Album.GenreId == 1.

Return Value

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

Exceptions

Exception Condition
FormatException
- or -
BadExpressionException
criteriais a malformed SimpleExpression
ArgumentException criteria has been assigned zero or more than one SimpleExpression

Remarks

The GetCount method is used instead of GetCountBy when the criteria is more complicated than simple equality tests. Criteria can be constructed just using regular C# expressions, with all operators supported and precedence-grouping honoured.

These C# expressions are represented internally as a SimpleExpression object. For more on SimpleExpressions, please read this page. Read also the Column Selection page for more on how to identify the columns in a SimpleExpression.

Examples

One Criteria

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

int albumCount = Database.Open().Albums.GetCount(db.Albums.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 albumCount = Database.Open().Albums
   .GetCount(db.Albums.GenreId == 1 && db.Albums.ArtistId == 120);

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

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