在 SQL 中进行乘法运算

Make a multiplication in a SQL

我要搜索return一个值,我要做的是两个字段相乘的总和。 我有以下代码:

internal double TotalRes(long Id)
{
     double total = 0;
     Reserve rAlias = null;
     var query = Session.QueryOver<Item>();
     query = query.JoinAlias(e => e.Reserve, () => rAlias);
     query = query.Where(() => rAlias.Id == Id);
     query = query.Select(Projections.Sum<Item>(acct => acct.Ammount * acct.Wight));
     object result = query.UnderlyingCriteria.UniqueResult();
     if (result != null)
         total = Convert.ToDouble(result);
     return total;
}

出现以下错误:

the variable 'acct' type 'tem' is referenced in scope '', but it is not set

我如何return这个值?

试试这个

Item rItem = null;
var query = Session.QueryOver<Item>(() => rItem);
...
query = query.Select(Projections.Sum(
        Projections.SqlFunction(new VarArgsSQLFunction("(", "*", ")"),
        NHibernateUtil.Double,
        Projections.Property(() => rItem.Ammount),
        Projections.Property(() => rItem.Wight))));

尝试使用公式在映射中做类似的事情。

Map(c => c.total).formula("(select sum(Ammount * Wight) from acct)");