1

nHibernate を使用したクエリに問題があります。
次のエンティティがあります。

public class CostAmount  
{  
    public virtual int Id {get;set;}  
    public virtual Year Year{get; set;}  
    public virtual Month Month{get; set;}  
}  

public class Year  
{  
    public virtual int Id {get;set;}  
    public virtual string Code {get;set;}  
}  

public class Month  
{  
    public virtual int Id {get;set;}  
    public virtual string Code {get;set;}  
}  

次のようなSQLを使用してクエリを実行したい:

select * from CostAmount ca  
inner join Year y on ca.YearID = y.ID  
inner join Month m on ca.MonthID = m.ID  
where y.Code *100+m.Code between 9107 and 9207  

誰でも私を助けてください。

4

2 に答える 2

2

私が書いたように、NHibernate QueryOver は数学演算の構文が貧弱です... 今、(通常は文字列に 100 を掛けないので)を考えるCodeと:int

// Aliases
CostAmount costAmount = null;
Year year = null;
Month month = null;

// Projections for the math operations

// y.Code * 100
var proj1 = Projections.SqlFunction(
    new VarArgsSQLFunction("(", "*", ")"),
    NHibernateUtil.Int32,
    Projections.Property(() => year.Code),
    Projections.Constant(100)
);

// proj1 + m.Code 
var proj2 = Projections.SqlFunction(
    new VarArgsSQLFunction("(", "+", ")"),
    NHibernateUtil.Int32,
    proj1,
    Projections.Property(() => month.Code)
);

// The query

var query = Session.QueryOver(() => costAmount)
                   .JoinAlias(() => costAmount.Year, () => year)
                   .JoinAlias(() => costAmount.Month, () => month)
                   .Where(Restrictions.Between(proj2, 9107, 9207));

var res = query.List();

数学演算のトリックはhttps://stackoverflow.com/a/10756598/613130から取られました

于 2013-08-29T10:32:46.783 に答える
0

たぶん、この質問を確認できますFluent Nibernate inner join

あるいは、この説明から正しい答えを見つけることができるかもしれません。 http://ayende.com/blog/4023/nhibernate-queries-examples

于 2013-08-29T09:57:16.473 に答える