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.

FindBy

FindBy is now deprecated in favour of using FindAllBy.FirstOrDefault() or Get. More details here.

FindBy returns a single row of data as a SimpleRecord object based on at least one column name and value to filter by; if more than one row matches, the first row retrieved from the data store is returned.

There are two ways to form a FindBy method.

Syntax

public SimpleRecord FindBy(
		Object[] filterExpressions
	)

public SimpleRecord FindBycolumnName1[AndcolumnName2…](
	Object columnValue1
	[, Object columnValue2]
)

Parameters

filterExpressions
Type: object[]
A set of named parameters where the parameter name is that of column being filtered against and the parameter value is that which the column should equal.
columnName1[, columnName2, …, columnNameX]
Type: string The name of the column(s) being filtered against
columnValue1[, columnValue2, …, columnValueX]
Type: object (typically string or int) columnValueY contains the value with which column with name columnNameY should equal.

Return Value

Type: SimpleRecord
An object containing a property for each of the columns requested by Simple.Data whose values are those of the single row retrieved from the data store.

Exceptions

Exception Condition
ArgumentException You have specified no named parameters for filterExpressions
-or-
The number of columnNames does not match the number of columnValues
NullReferenceException One of the columnValues has been set to null

Remarks

FindBy is used to return data from a table based on a set of criteria where the value of columns in a row

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 FindBy. If you have a column called, for example, HandlingCode, and you try to call

FindByHandlingCode(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,

FindBy(HandlingCode:1);

Examples

The following example retrieves the first album with GenreId equal to 1 and outputs its title to the Console window.

var album = Database.Open().Albums.FindByGenreId(1);
	
// The named parameter equivalent call to FindBy() is
// var album = Database.Open().Albums.FindBy(GenreId:1);
	
Console.WriteLine(album.Title);

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

SELECT TOP 1 
    [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 the first album with GenreId equal to 1 and ArtistId equal to 120 and outputs its title to the Console window.

var album = Database.Open().Albums.FindByGenreIdAndArtistId(1,120);
	
// The named parameter equivalent call to FindBy() is
// var album = Database.Open().Albums.FindBy(GenreId:1,ArtistId:120);
	
Console.WriteLine(album.Title);

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

SELECT TOP 1 
    [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