的別名SqlProjection我有這種情況:創建連接表
public class AnswerSet {
public virtual IList<Answer> Answers {set; get;}
}
session.CreateCriteria<AnswerSet>()
.CreateAlias("Answers", "a")
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("TopicId"), "TopicId")
.Add(Projections.GroupProperty("TopicName"), "TopicName")
.Add(Projections.SqlProjection("count (case **{a}**.numerical_answer
when 1 then 1 when -1 then 1 else null end) as YesAnswers",
new[] { "YesAnswers" }, new IType[] { NHibernateUtil.Int32 }), "YesAnswers")
如何指定子集的別名? {a} .numerical_answer不工作,{別名}引用AnswerSet。
的SQL相當於是
select
[as].topic_id as TopicId
, [as].topic_name as TopicName
, count (case [a].numerical_answer
when 1 then 1
when -1 then 1
else null
end) as YesAnswers
from answer_set [as] join answer [a] on [a].answer_set_id = [as].id
謝謝
丹尼爾
這樣做是等待發生的災難。這可以在今天使用,但是當稍後將新列添加到數據庫時,可能會在沒有人注意的情況下中斷該查詢。看看這裏:http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/10/25/defensive-database-programming-qualifying-column-names.aspx – Alejandro 2016-06-29 17:12:32
有效的關注。截至今天,我可能會解決這個問題,通過使用QueryOver並在代碼中計算所需的結果,而不是首先有很多魔術字符串。 – 2016-07-18 09:33:41