views:

210

answers:

0

NHibernate has an attribute on the property element in the mapping config named "formula" that allows the injections of sql to "calculate" a property. The issue I have is the formula using sql syntax directly. Is there a way to have nhibernate.linq to use a lambda expression instead of using the formula property.

I have the following:

public class Invoice
{
    public virtual int Id { get; protected set; }
    public virtual decimal Amount { get; set; }
    public virtual decimal Paid { get; set; }
    public virtual decimal Balance
    {
        get { return BalanceExpression.Expression.Compile().Invoke(this); }
    }
}

public class BalanceExpression
{
    public static Expression<Func<Invoice, decimal>> Expression
    {
        get { return i => i.Amount - i.Paid; }
    }
}

<class name="Invoice"> 
  <id name="Id"> 
    <generator class="hilo"/> 
  </id> 
  <property name="Amount"/> 
  <property name="Paid"/> 
  <property name="Balance" formula="Amount - Paid" access="readonly"/> 
</class>

I want nhibernate to use the balanceexpression.expression instead of having to put sql syntax in the formula attribute so I can remove the formula attribute from my mapping config and write queries as follows:

from i in session.linq() where i.balance > 0 select i;

How do I inject the balanceexpression.expression into the linq query?