tags:

views:

136

answers:

1

I've made POCO object mapped to database like this:

class MoneyObject
{
    Money MoneyAmmount { get;set; }
    string Description { get;set; } 
}

MoneyAmmount - is type Money which is derived from ICompositeUserType and has 2 properties decimal Amount and string CurrencyCode:

class Money
{
    decimal Ammount { get;set; }
    string CurrencyCode { get;set; } 
}

Problem appears when I'm trying to fetch data from database using criteria, like following:

var criteria = session.CreateCriteria(typeof(MoneyObject))
    .Add(Expression.Lt("MoneyAmmount", money));

It generates following sql query:

SELECT this_.Id as Id2_0_, this_.Value as Value2_0_, this_.Currency as Currency2_0_, this_.Description as Descript4_2_0_ 
FROM MoneyObject this_ 
WHERE this_.Value < @p0 and this_.Currency < @p1;@p0 = 300,01, @p1 = 'USD'

I don't want to compare my valuetype object Money using CurrecyCode by character. I want to compare Money only by amount property and fetch data using criteria. But dealing in criteria only with properties of POCO object. Means I know that comparision works:

Expression.Lt("MoneyAmmount.Ammount", money.Ammount)

I want to avoid comparison by valuetype object properties and achieve comparison only between valuetype objects like

Expression.Lt("MoneyAmmount", money)

Does anybody know how to change such comparing behavior?

+2  A: 

Try something like this

var criteria = session.CreateCriteria(typeof(MoneyObject), "m")
                             .CreateAlias("m.MoneyAmmount", "a")
                             .Add(Expression.Lt("a.Ammount", money));

Creating the aliases "m" and "a" lets you compare to the nested property.

Mark Dickinson
Yes I know about it, but I want to compare Money objects by Ammount value and not a decimal Ammount property. I need compare valuetype objects by numeric properties or by custom logic because I am dealing with large amount of different composite types.
Regfor
Sorry, the question says you want to compare Money by amount property. Could you tell us what the variable 'money' is? Sorry if I'm not understanding.
Mark Dickinson
Unless I'm missing something really obvious, your first criteria expression is correct, the sql generated is always going to compare on both properties of your POCO object, the database only holds the data, so it has to compare on both if you only want to match on Money objects in the MoneyAmmount property. Sorry if this is not helpful.
Mark Dickinson
I think there is no possibility to compare composite valuetype objects by custom logic, only by all properties like in generated sql query. So I'll use expression like Expression.Lt("MoneyAmmount.Ammount", money.Ammount)or .CreateAlias("m.MoneyAmmount", "a").Add(Expression.Lt("a.Ammount", money));Thanks
Regfor