By default, the All, Get
and Find* commands retrieve every column from each table
specified in their invocation. Use the Select method to set explicitly
the data fields returned by those commands.
public SimpleQuery Select( [object[] columnNames] )
columnNamesobject[]
Type: SimpleQuery
A SimpleQuery object containing a Select clause.
| Exception | Condition |
|---|---|
UnresolvableObjectException
|
You have included a non-existent column in the Select
method
|
InvalidOperationException
|
You have tried to call Select twice in the same command chain
|
The Select method can qualify any query command. It
can be daisychained to the target command or called separately.
You can also call it on its own, at which point Simple.Data will imply a call to
All.
If you supply no arguments to Select, Simple.Data will return all columns.
Select is not cumulative. Calling it a second time in the same command chain with additional columns to include will throw an InvalidOperationException.
Columns can be identified in any order. Their names will be resolved according to the same rules as those for resolving the names of tables:
You can also use an indexed syntax to identify schemas, tables and columns at runtime rather than the fluid syntax which requires compile time decisions. For example,
db.SchemaName.TableName.ColumnName can also be written as db["SchemaName"]["TableName"]["ColumnName"]. See the examples
below for comparative code.
Should you reference columns not enumerated in a Select
method - for example, trying to access the GenreId
column after selecting only AlbumId and Title - throws a Simple.Data.UnresolvableObjectException
exception
Simple.Data defines two synonymous methods, AllColumns and Star, which when appended to a table reference will return all columns for that table. This comes in handy in join queries where you may want to return all the fields in one table but not in another. See Example 3 for the syntax.
To return all the AlbumId and Title fields from the Albums table, use
one of the following commands
// Using fluid syntax
var albums = db.Albums.All()
.Select(
db.Albums.AlbumId,
db.Albums.Title);
var albums = db.Albums.Select(
db.Albums.AlbumId,
db.Albums.Title);
// Using indexed syntax
var albums = db["Albums"].All()
.Select(
db["Albums"]["AlbumId"],
db["Albums"]["Title"]);
var albums = db["Albums"].Select(
db["Albums"]["AlbumId"],
db["Albums"]["Title"]);
Simple.Data sends the following SQL to the database when albums is evaluated.
select [dbo].[Album].[AlbumId], [dbo].[Album].[ArtistId] from [dbo].[Album]
To return all the AlbumId and Title fields from the Albums table where
GenreId equals 1, use one of the following commands.
// Using fluid syntax
db.Albums.FindAllByGenreId(1)
.Select(
db.Albums.AlbumId,
db.Albums.Title)
);
db.Albums.FindAll(db.Albums.GenreId == 1)
.Select(
db.Albums.AlbumId,
db.Albums.Title)
);
// Using indexed syntax
db["Albums"].FindAllByGenreId(1)
.Select(
db["Albums"]["AlbumId"],
db["Albums"]["Title"]);
);
db["Albums"].FindAll(db["Albums"].GenreId == 1)
.Select(
db["Albums"]["AlbumId"],
db["Albums"]["Title"]);
);
Either will produce the following SQL
SELECT
[dbo].[Album].[AlbumId],
[dbo].[Album].[Title]
from [dbo].[Album]
where [dbo].[Album].[GenreId] = @p1
@p1 (Int32) = 1
To return all the fields from the Albums table using the AllColumns or Star methods, use the following syntax.
db.Albums.All() .Select(db.Albums.Star()); db.Albums.All() .Select(db.Albums.AllColumns());
Either will produce the following SQL
SELECT [dbo].[Albums].[AlbumId], [dbo].[Albums].[GenreId], [dbo].[Albums].[ArtistId], [dbo].[Albums].[Title], [dbo].[Albums].[Price], [dbo].[Albums].[AlbumArtUrl] from [dbo].[Albums]