Make a multiplication in a SQL
Asked Answered
C

2

6

I have to do a search to return a value , I have to do is the sum of multiplying two fields. I have the following code:

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;
}

It is giving the following error:

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

How can i return this value?

Condenser answered 24/8, 2015 at 13:7 Comment(4)
Is Projections your class or is it part of nhibernate? (Never used the library)Cwm
possible duplicate of Expression.Lambda: Variable 'x' of type '' referenced from scope '', but it is not definedSola
Its part of nhibernate @ScottChamberlainCondenser
What happens if you get the Ammount and the Wight from the query and then do the multiplication? My thought is that Projections does not know what acct.Ammount or acct.Wight is.Gable
H
1

Try to do something like this in the mapping, using formula.

Map(c => c.total).formula("(select sum(Ammount * Wight) from acct)");
Hookworm answered 19/6, 2017 at 20:15 Comment(0)
K
1

Try this

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))));
Khanate answered 6/11, 2015 at 18:12 Comment(0)
H
1

Try to do something like this in the mapping, using formula.

Map(c => c.total).formula("(select sum(Ammount * Wight) from acct)");
Hookworm answered 19/6, 2017 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.