2015-08-24 138 views
6

Tôi phải thực hiện tìm kiếm để trả về một giá trị, tôi phải làm là tổng số nhân hai trường. Tôi có đoạn mã sau:Thực hiện phép nhân trong SQL

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

Nó được đưa ra các lỗi sau:

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

Làm thế nào tôi có thể quay trở lại giá trị này?

+1

Có phải 'Chiếu' lớp học của bạn hay là một phần của nhibernate? (Không bao giờ sử dụng thư viện) –

+0

bản sao có thể có của [Expression.Lambda: Biến 'x' của loại '' được tham chiếu từ phạm vi '', nhưng nó không được xác định] (http://stackoverflow.com/questions/9778749/expression- lambda-variable-x-of-type-tham chiếu-từ-phạm vi-nhưng-it-is-n) –

+0

Một phần của nhibernate @ScottChamberlain –

Trả lời

1

Cố gắng làm một cái gì đó như thế này trong các bản đồ, sử dụng công thức.

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

Hãy thử điều này

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)))); 
Các vấn đề liên quan