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.
public bool Exists( [SimpleExpression criteria] )
criteriaSimpleExpressiondbo.Album.GenreId == 1.
Type: bool
A value indicating whether records exists in the table that satisfy the conditions in the SimpleExpression.
| 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.
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.
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]
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
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