FindAllBy returns all data from a table based on at least one column name and value to filter by.
There are two ways to form a FindAllBy method.
public SimpleQuery FindAllBy( Object[] filterExpressions ) public SimpleQuery FindAllBycolumnName1[AndcolumnName2…]( Object columnValue1 [, Object columnValue2] )
filterExpressionsobject[]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: SimpleQuery
A collection of SimpleRecord objects which can be iterated over.
| 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 |
FindAllBy is used to return all the data from a table based on a set of criteria where the value of columns in a row
Artistid=6 and Title="Back in Black"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 FindAllBy. If you have a column called, for example, HandlingCode, and you try to call
FindAllByHandlingCode(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,
FindAllBy(HandlingCode:1);
The following example retrieves all the albums with GenreId equal to 1 and outputs their titles to the Console window.
var albums = Database.Open().Albums.FindAllByGenreId(1);
// The named parameter equivalent call to FindAllBy() is
// var albums = Database.Open().Albums.FindAllBy(GenreId:1);
foreach (var album in albums)
{
Console.WriteLine(album.Title);
}
Simple.Data sends the following SQL to the database when album is evaluated.
SELECT
[dbo].[Album].[AlbumId],
[dbo].[Album].[GenreId],
[dbo].[Album].[ArtistId],
[dbo].[Album].[Title],
[dbo].[Album].[Price],
[dbo].[Album].[AlbumArtUrl]
from [dbo].[Album]
where
[dbo].[Album].[GenreId] = @p1
@p1 (Int32) = 1
The following example retrieves all the albums with GenreId equal to 1 and ArtistId equal to 120 and outputs their titles to the Console window.
var albums = Database.Open().Albums.FindAllByGenreIdAndArtistId(1,120);
// The named parameter equivalent call to FindAllBy() is
// var albums = Database.Open().Albums.FindAllBy(GenreId:1,ArtistId:120);
foreach (var album in albums)
{
Console.WriteLine(album.Title);
}
Simple.Data sends the following SQL to the database when album is evaluated.
SELECT
[dbo].[Album].[AlbumId],
[dbo].[Album].[GenreId],
[dbo].[Album].[ArtistId],
[dbo].[Album].[Title],
[dbo].[Album].[Price],
[dbo].[Album].[AlbumArtUrl]
from [dbo].[Album]
where
([dbo].[Album].[GenreId] = @p1 AND [dbo].[Album].[ArtistId] = @p2)
@p1 (Int32) = 1
@p2 (Int32) = 120