问题描述
我尝试使用SQL函数 CONSTAINS
来过滤QueryOver API上的一些数据。
主要问题是我不能在where子句中使用 SqlFunction
,它不会编译,因为需要 ICriterion
。
var result = Session.QueryOver< Individual>()
.Where(Projections.SqlFunction(
FullTextContains,NHibernateUtil.Boolean,
Projections.Property< Individual>(x => x.LastName),
Projections.Constant(something)))
.List() ;
我试图将它与 TRUE
常量,但执行查询时会产生语法错误,因为 CONSTAINS
函数不能与等号运算符一起使用。
var result = Session.QueryOver< Individual>()
.Where(Restrictions.Eq(Projections.SqlFunction
FullTextContains,NHibernateUtil.Boolean,
Projections.Property< Individual>(p => p.LastName),
Projections.Constant(something)),true))
.List();
如何直接使用布尔sql函数 strong> QueryOver API ?
这是我如何使用它的方式:
$ b Projection.Property< Individual>(x = 1) > x.LastName),Projections.Constant(something));
var result = Session.QueryOver< Individual>()
.Where(new ProjectionAsCriterion(projection))
.List();
使用 IProjection
作为 ICriterion
我根据NHibernate项目中的 SimpleExpression
类创建自己的实现。
public class ProjectionAsCriterion:AbstractCriterion
{
private readonly IProjection _projection;
public ProjectionAsCriterion(IProjection projection)
{
_projection = projection;
$ b public override SqlString ToSqlString(ICriteria criteria,ICriteriaQuery criteriaQuery,
IDictionary< string,IFilter> enabledFilters)
{
var columnNames = CriterionUtil。 GetColumnNamesForSimpleExpression(
null,_projection,criteriaQuery,criteria,enabledFilters,this,string.Empty);
var sqlBuilder = new SqlStringBuilder(4 * columnNames.Length);
for(int i = 0; i< columnNames.Length; i ++)
{
if(i> 0)
{
sqlBuilder .Add(和);
}
sqlBuilder.Add(columnNames [i]);
}
返回sqlBuilder.ToSqlString();
$ b public override TypedValue [] GetTypedValues(ICriteria criteria,ICriteriaQuery criteriaQuery)
{
var typedValues = new List< TypedValue>();
if(_projection!= null)
{
typedValues.AddRange(_projection.GetTypedValues(criteria,criteriaQuery));
}
typedValues.Add(GetParameterTypedValue(criteria,criteriaQuery));
返回typedValues.ToArray();
$ b private TypedValue GetParameterTypedValue(ICriteria criteria,ICriteriaQuery criteriaQuery)
{
return CriterionUtil.GetTypedValues(criteriaQuery,criteria,_projection,null).Single();
}
public override IProjection [] GetProjections()
{
return new [] {_projection};
}
public override string ToString()
{
return _projection.ToString();
}
}
I'm trying to use the SQL function CONSTAINS
to filter some data on QueryOver API.
The main issue is i can't use SqlFunction
in where clause, it does not compile, because a ICriterion
is needed.
var result = Session.QueryOver<Individual>()
.Where(Projections.SqlFunction(
"FullTextContains", NHibernateUtil.Boolean,
Projections.Property<Individual>(x => x.LastName),
Projections.Constant("something")))
.List();
I tried to match it to a TRUE
constant, but when the query is executed it generates syntax error, because CONSTAINS
function can't be used with equals operator.
var result = Session.QueryOver<Individual>()
.Where(Restrictions.Eq(Projections.SqlFunction(
"FullTextContains", NHibernateUtil.Boolean,
Projections.Property<Individual>(p => p.LastName),
Projections.Constant("something")), true))
.List();
How can i use a boolean sql function directly in where expression on QueryOver API?
This is how i found how to use it:
var projection = Projections.SqlFunction("FullTextContains",
NHibernateUtil.Boolean,
Projections.Property<Individual>(x => x.LastName),
Projections.Constant("something"));
var result = Session.QueryOver<Individual>()
.Where(new ProjectionAsCriterion(projection))
.List();
To use a IProjection
as a ICriterion
i create my own implementation based on SimpleExpression
class from NHibernate project.
public class ProjectionAsCriterion : AbstractCriterion
{
private readonly IProjection _projection;
public ProjectionAsCriterion(IProjection projection)
{
_projection = projection;
}
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
IDictionary<string, IFilter> enabledFilters)
{
var columnNames = CriterionUtil.GetColumnNamesForSimpleExpression(
null, _projection, criteriaQuery, criteria, enabledFilters, this, string.Empty);
var sqlBuilder = new SqlStringBuilder(4 * columnNames.Length);
for (int i = 0; i < columnNames.Length; i++)
{
if (i > 0)
{
sqlBuilder.Add(" and ");
}
sqlBuilder.Add(columnNames[i]);
}
return sqlBuilder.ToSqlString();
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var typedValues = new List<TypedValue>();
if (_projection != null)
{
typedValues.AddRange(_projection.GetTypedValues(criteria, criteriaQuery));
}
typedValues.Add(GetParameterTypedValue(criteria, criteriaQuery));
return typedValues.ToArray();
}
private TypedValue GetParameterTypedValue(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
return CriterionUtil.GetTypedValues(criteriaQuery, criteria, _projection, null).Single();
}
public override IProjection[] GetProjections()
{
return new[] { _projection };
}
public override string ToString()
{
return _projection.ToString();
}
}
这篇关于如何将全文搜索用于具有QueryOver API的任何属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!