Hi Graeme,
The Convert.ToDecimal
is converted to an equivalent CONVERT/CAST sql code inline in the query. The Convert.ToDecimal
doesn't apply any precision/scale value as the .NET Decimal type doesn't have a scale/precision setting as well. If you require a specific precision/scale specification with the conversion in SQL, please create a custom function mapping for Convert.ToDecimal() and map it to the proper CONVERT statement compatible to your DB of choice. This is mentioned in this documentation page.
Something like this:
internal class NorthwindFunctionsQS
{
public static FunctionMappingExpression CastAsDecimal(IEntityFieldCore field)
{
return new FunctionMappingExpression(typeof(NorthwindFunctionsQS), "CastAsDecimal", 1, field);
}
}
internal class NorthwindFunctionMappingsQS : FunctionMappingStore
{
public NorthwindFunctionMappings()
: base()
{
// define the mapping.
this.Add(new FunctionMapping(typeof(NorthwindFunctionsQS), "CastAsDecimal", 1, "CAST({0} AS DECIMAL(7,2))", "Northwind", "dbo"));
}
}
Then use this function in your query:
var q = (from cob in md.CalculatedOrderBilling
join o in md.Order on cob.OrderId equals o.OrderId
where cob.OrderId == OrderId
select new BillingCharge
{
Rate = NorthwindFunctionsQS.CastAsDecimal(cob.Rate).ToValue<decimal>()
});
q.CustomFunctionMappingStore = new NorthwindFunctionMappingsQS();
var results = adapter.FetchQuery(q);
Hope that helps