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. For example, the As method allows columns being returned from a data store to be named something else.

As

Allows you to set an alias for a column being selected in a query.

Syntax

public ObjectReference As(
		string alias
	)

Parameters

alias
Type: string
A comma-separated list of fields to be retrieved by the main query command.

Return Value

Type: ObjectReference
The new name with which to reference a data field within a SimpleRecord once it has been retrieved from a data store.

Exceptions

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

Remarks

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.

Examples

Aliasing a Table Column

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]

Aliasing a column Operation

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]