Allows you to set an alias for a column being selected in a query.
public ObjectReference As( string alias )
alias
string
Type: ObjectReference
The new name with which to reference a data field within a SimpleRecord
once it has been retrieved from a data store.
Exception | Condition |
---|---|
ArgumentException
|
You have set a field’s alias to the name or alias of another field.
|
System.Collections.Generic.KeyNotFoundException
|
You have set a field’s alias to one value and then tried to access the field with its original name.
|
[ADO Adapter Only]Simple.Data.Ado.AdoAdapterException
|
You have set alias to a non-string value
|
The ability to set data field aliases is of particular use when tables are joined
together in a query and the possibility of column name ambiguity exists; you might
want to select two columns with the same name from two tables. To cope with this,
assign aliases to columns within the selection using the As
method.
Should two data fields to the same alias
or alias
one field to the name of another, Simple.Data will throw a System.ArgumentException
when the query is resolved.
To refer to the Album.Title field as ‘AlbumName’ in your code, use the following command.
var albums = db.Albums.All() .Select( db.Albums.AlbumId, db.Albums.Title.As("AlbumName"));
Simple.Data sends the following SQL to the database when albums is evaluated.
select [dbo].[Album].[AlbumId], [dbo].[Album].[ArtistId] AS [AlbumName] from [dbo].[Album]
You can alias the addition of (or any other operation over) two columns using Select
and As
.
db.OrderDetails.All() .Select( db.OrderDetails.Quantity * db.OrderDetails.UnitPrice).As("SubTotal"));
Simple.Data sends the following SQL to the database when albums is evaluated.
select ([dbo].[OrderDetails].[Quantity] * [dbo].[OrderDetails].[UnitPrice]) AS [SubTotal] from [dbo].[OrderDetails]