不带硬编码列名的Nhibernate查询版本排序规则
因此,我有以下sql 从名称对照拉丁语1_GENERAL_CI_AI如“myText%”的表格中选择* 我想用QueryOver实现它 目前我有:不带硬编码列名的Nhibernate查询版本排序规则,nhibernate,queryover,Nhibernate,Queryover,因此,我有以下sql 从名称对照拉丁语1_GENERAL_CI_AI如“myText%”的表格中选择* 我想用QueryOver实现它 目前我有: whereRestriction.Add(Expression.Sql("Name COLLATE LATIN1_GENERAL_CI_AI LIKE ?", String.Format("{0}%", subStringMatch), HibernateUtil.String)); 这很好,但有两个问题。首先,它是特定于sqlserver的,其次
whereRestriction.Add(Expression.Sql("Name COLLATE LATIN1_GENERAL_CI_AI LIKE ?", String.Format("{0}%", subStringMatch), HibernateUtil.String));
这很好,但有两个问题。首先,它是特定于sqlserver的,其次,数据库列“Name”是硬编码的
有没有人有任何建议来解决这两个问题,或者至少是硬编码的db列名 我是这样实现的。不确定是否有更好的方法 I.like表达式,从现有like表达式中获益
public class LikeCollationExpression : LikeExpression
{
const string CollationDefinition = " COLLATE {0} ";
const string Latin_CI_AI = "LATIN1_GENERAL_CI_AI";
// just a set of constructors
public LikeCollationExpression(string propertyName, string value, char? escapeChar, bool ignoreCase) : base(propertyName, value, escapeChar, ignoreCase) { }
public LikeCollationExpression(IProjection projection, string value, MatchMode matchMode) : base(projection, value, matchMode) { }
public LikeCollationExpression(string propertyName, string value) : base(propertyName, value) { }
public LikeCollationExpression(string propertyName, string value, MatchMode matchMode) : base(propertyName, value, matchMode) { }
public LikeCollationExpression(string propertyName, string value, MatchMode matchMode, char? escapeChar, bool ignoreCase) : base(propertyName, value, matchMode, escapeChar, ignoreCase) { }
// here we call the base and append the COLLATE
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
{
// base LIKE
var result = base.ToSqlString(criteria, criteriaQuery, enabledFilters);
var sqlStringBuilder = new SqlStringBuilder(result);
// extend it with collate
sqlStringBuilder.Add(string.Format(CollationDefinition, Latin_CI_AI ));
return sqlStringBuilder.ToSqlString();
}
}
我是这样实现的。不确定是否有更好的方法 I.like表达式,从现有like表达式中获益
public class LikeCollationExpression : LikeExpression
{
const string CollationDefinition = " COLLATE {0} ";
const string Latin_CI_AI = "LATIN1_GENERAL_CI_AI";
// just a set of constructors
public LikeCollationExpression(string propertyName, string value, char? escapeChar, bool ignoreCase) : base(propertyName, value, escapeChar, ignoreCase) { }
public LikeCollationExpression(IProjection projection, string value, MatchMode matchMode) : base(projection, value, matchMode) { }
public LikeCollationExpression(string propertyName, string value) : base(propertyName, value) { }
public LikeCollationExpression(string propertyName, string value, MatchMode matchMode) : base(propertyName, value, matchMode) { }
public LikeCollationExpression(string propertyName, string value, MatchMode matchMode, char? escapeChar, bool ignoreCase) : base(propertyName, value, matchMode, escapeChar, ignoreCase) { }
// here we call the base and append the COLLATE
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
{
// base LIKE
var result = base.ToSqlString(criteria, criteriaQuery, enabledFilters);
var sqlStringBuilder = new SqlStringBuilder(result);
// extend it with collate
sqlStringBuilder.Add(string.Format(CollationDefinition, Latin_CI_AI ));
return sqlStringBuilder.ToSqlString();
}
}
伟大的享受NHibernate。这是一个很棒的工具;伟大的享受NHibernate。这是一个很棒的工具;
...
query.WhereLikeCiAi(c => c.Name, "searchedString", MatchMode.Anywhere);