A Quick "Cast" Sample
The casting logic is now in-place for the EntitySpaces 2008 March 30 Beta release. Let's look at a few quick examples, so you can get a feel for how this new feature works. These examples are not real world scenarios, and were chosen to merely demonstrate the syntax.
EmployeeCollection coll = new EmployeeCollection();
EmployeeQuery q = coll.Query;
q.Select
(
(
(q.LastName + ", " + q.FirstName).Trim() + " [" + (esString)q.Age + "]"
)
.ToUpper().As("FirstName")
);
if (coll.Query.Load())
{
foreach (Employee emp in coll)
{
string fn = emp.FirstName;
}
}
The sample above is a little over the top, but we wanted to show you some of the features. Notice that we Trim the results of ("LastName+ ", " + FirstName") by placing parenthesis around it, and then using Trim. However, we're not finished yet. We then continue by concatenating the Age. We cast Age into a string using the (esString) cast operator. You can use our natural language syntax throughout the EntitySpaces DynamicQuery API.
The resulting values for FirstName would be as follows:
SMITH, JOHN [30]
DOE, JANE [20]
The SQL generated for the Query above would be as follows:
SELECT UPPER((((LTRIM(RTRIM((([LastName]+', ')+[FirstName])))+' [')+CAST([Age] AS nvarchar))+']')) AS 'FirstName' FROM [Employee]
If you match-up the color coding from the EntitySpaces DynamicQuery to the resulting SQL, you can see that the (esString) resulted in a call to Cast(). Note how at various points we were able to group things with parenthesis to apply further processing, for example, Trim and ToUpper. Finally, we end the query by providing our virtual column with an alias. We even aliased it back to our FirstName column, so we could access it directly with a property instead of using GetColumn().
Supported Cast Operators
The cast operators supported are as follows:
- esBoolean
- esByte
- esChar
- esDateTime
- esDecimal
- esDouble
- esGuid
- esInt16
- esInt32
- esInt64
- esSingle
- esString
More Advanced Casting
In cases where you need a little more control, you can use the Cast() method in the query explicitly. There are three methods available.
- Cast(esCastType castType) - Really offers nothing over the inline casting shown above
- Cast(esCastType castType, int length) - useful when casting strings
- Cast(esCastType castType, int precision, int scale) - useful for numeric/decimal types
Forgive us, if these samples aren't quite real world samples:
Employee emp = new Employee();
emp.Query.Select(emp.Query.Age.Cast(esCastType.Decimal, 8, 4).As("CastColumn"));
emp.Query.es.Top = 1;
if(emp.Query.Load())
{
}
This results in:
SELECT TOP 1 CAST([Age] AS decimal(8,4)) AS 'CastColumn' FROM [Employee]
If you didn't need to control the precision and scale, you could just use the syntax shown below:
Employee emp = new Employee();
emp.Query.Select((esDecimal)emp.Query.Age.As("CastColumn"));
emp.Query.es.Top = 1;
if(emp.Query.Load())
{
}
This results in:
SELECT TOP 1 CAST([Age] AS decimal) AS 'CastColumn' FROM [Employee]
Summary
Our new casting support, combined with our new SubQuery support, should add to the horsepower already provided by EntitySpaces 2007. EntitySpaces 2008 will give you a real boost when it comes to writing your EntitySpaces applications. EntitySpaces 2008 extends the DynamicQuery API significantly. All of these features are supported for all of the EntitySpaces databases.
From mobile devices to large scale enterprise solutions in need of serious transaction support, EntitySpaces can meet your needs. Whether you’re writing an ASP.NET application with medium trust requirements, a Mono application, or a Windows.Forms application, the EntitySpaces architecture is there for you. EntitySpaces is provider independent, which means that you can run the same binary code against any of the supported databases. EntitySpaces is available in both C# and VB.NET. EntitySpaces uses no reflection, no XML files, and sports a tiny foot print of less than 200k. Pound for pound, EntitySpaces is one tough, dependable .NET architecture.
The EntitySpaces Team
--
EntitySpaces LLC
Persistence Layer and Business Objects for Microsoft .NET
http://www.entityspaces.net