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.

Exists

Exists returns a boolean value based on whether a search returns any values. An optional SimpleExpression defines the criteria for the search. Any is a synonym for Exists.

Syntax

public bool Exists(
	[SimpleExpression criteria]
)

Parameters

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

Return Value

Type: bool
A value indicating whether records exists in the table that satisfy the conditions in the SimpleExpression.

Exceptions

Exception Condition
UnresolvableObjectException Exists is called on a table that doesn’t exist
FormatException
- or -
BadExpressionException
criteriais a malformed SimpleExpression
ArgumentException criteria has been assigned zero or more than one SimpleExpression

Note that issues #315, #316, and #317 are still open with regards to exceptions thrown by Exists.

Remarks

The Exists method can be used either as the main command in a Simple.Data query or as a daisychained qualifier. For example, the following quads of queries should produce the same results.

bool albumsExist = Database.Open.Albums.All().Exists();
bool albumsExist = Database.Open.Albums.Exists();
bool albumsExist = Database.Open.Albums.All().Any();
bool albumsExist = Database.Open.Albums.Any();

bool darkSideExists = Database.Open.Albums.FindAll(db.Albums.Title == "Dark Side of the Moon").Exists();
bool darkSideExists = Database.Open.Albums.Exists(db.Albums.Title == "Dark Side of the Moon");
bool darkSideExists = Database.Open.Albums.FindAll(db.Albums.Title == "Dark Side of the Moon").Any();
bool darkSideExists = Database.Open.Albums.Any(db.Albums.Title == "Dark Side of the Moon");

The Exists method is used instead of ExistsBy 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

No Criteria

The following example returns true if there are any records in the Albums table.

bool albumExists = Database.Open().Albums.Exists();

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

select DISTINCT 1 from [dbo].[Albums]

One Criteria

The following example returns true if there are any records in the Albums table with GenreId equal to 1.

bool albumExists = Database.Open().Albums.Exists(db.Albums.GenreId == 1);

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

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

Two Criteria

The following example returns true if there are any records in the Albums table with GenreId equal to 1 and ArtistId equal to 120.

bool albumExists = Database.Open().Albums
   .Exists(db.Albums.GenreId == 1 && db.Albums.ArtistId == 120);

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

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