我正在调用sql server函数包含如下:
ftquery = _OrElse(ftquery,Restrictions.Eq(Projections.SqlFunction("contains",NHibernateUtil.Boolean, Projections.Property<Document>(d => d.SearchContent), Projections.Constant(query.Query)),true));
OrElse将执行或使用Restrictions.Or(ICriterion,ICriterion)。问题是它生成了无效的sql:
... and contains(this_.SearchContent, ?) = ? ORDER BY ...
我不想拥有正确的部分(=?),我只需要没有Restrictions.Eq的投影,但没有Restrictions.Eq我找不到任何解决方案将投影转换为标准。< / p>
我们如何使用NHibernate生成类似的SQL:
Select Name from Users where Contains(Name,'toto') or Contains(Job,'tata')
答案 0 :(得分:0)
在您的方言中注册该功能:
RegisterFunction("FullTextContains", new StandardSQLFunction("contains", NHibernateUtil.Boolean));
使用Projections.SqlFunction
创建投影,然后在查询中使用此ProjectionAsCriterion类:
var projection = Projections.SqlFunction("FullTextContains",
NHibernateUtil.Boolean,
Projections.Property<Document>(x => x.SearchContent),
Projections.Constant(query.Query));
var result = Session.QueryOver<Document>()
.Where(new ProjectionAsCriterion(projection))
.List();