ExistsBy
returns a boolean value based on whether a search returns any values based on at least one column name and value to filter by. AnyBy
is a synonym for ExistsBy
.
There are two ways to form a ExistsBy
method.
public bool ExistsBy( Object[] filterExpressions ) public bool ExistsBycolumnName1[AndcolumnName2…]( Object columnValue1 [, Object columnValue2] )
filterExpressions
object[]
columnName1[, columnName2, …, columnNameX]
string
The name of the column(s) being filtered againstcolumnValue1[, columnValue2, …, columnValueX]
object
(typically string
or int
)
columnValueY
contains the value with which column with name columnNameY
should equal.
Type: bool
A value indicating whether records exists in the table that satisfy the given conditions.
Exception | Condition |
---|---|
UnresolvableObjectException |
ExistsBy is called on a table that doesn’t exist |
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 |
Note that issues #318, #319, and #320 are still open with regards to exceptions thrown by ExistsBy
.
ExistsBy is used to determine whether data exists in a table based on a set of criteria where the value of columns in a row
GenreId=6
and Title="Nevermind"
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 ExistsBy
. If you have a column called, for example, HandlingCode
, and you try to call
ExistsByHandlingCode(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,
ExistsBy(HandlingCode:1);
The following example returns true if there are any records in the Albums table with GenreId
equal to 1
.
bool albumExists = Database.Open().Albums.ExistsByGenreId(1); // The named parameter equivalent call to ExistsBy() is // bool albumExists = Database.Open().Albums.ExistsBy(GenreId:1); // Also equivalent are // bool albumExists = Database.Open().Albums.AnyByGenreId(1); // bool albumExists = Database.Open().Albums.AnyBy(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.ExistsByGenreIdAndArtistId(1,120); // The named parameter equivalent call to ExistsBy() is // bool albumExists = Database.Open().Albums.ExistsBy(GenreId:1,ArtistId:120); // Also equivalent are // bool albumExists = Database.Open().Albums.AnyByGenreIdAndArtistId(1,120); // bool albumExists = Database.Open().Albums.AnyBy(GenreId:1,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