NHibernate IsInsensitiveLike将字符串视为区分大小写

时间:2015-05-07 16:10:26

标签: c# database nhibernate

您好我必须遵循代码

CurrentSession.QueryOver<Entity>()
.WhereRestrictionOn(x => x.Name).IsInsensitiveLike(filterValue,MatchMode.Anywhere)
.List()

据我所知,比较应该不区分大小写,但如果名称为“Country”,则“country”一词不会返回任何匹配。

此代码表达式有什么问题?

谢谢,

1 个答案:

答案 0 :(得分:0)

信息太少但总的来说 - 这应该(几乎必须)工作。此功能由

实现

InsensitiveLikeExpression.cs

这是片段

public override SqlString ToSqlString(ICriteria criteria
  , ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
{
    //TODO: add default capacity
    SqlStringBuilder sqlBuilder = new SqlStringBuilder();
    SqlString[] columnNames =
        CriterionUtil.GetColumnNames(propertyName, projection
                                    , criteriaQuery, criteria, enabledFilters);

    if (columnNames.Length != 1)
    {
        throw new HibernateException("insensitive like may only " +
            " be used with single-column properties");
    }

    if (criteriaQuery.Factory.Dialect is PostgreSQLDialect)
    {
        sqlBuilder.Add(columnNames[0]);
        sqlBuilder.Add(" ilike ");
    }
    else
    {
        sqlBuilder.Add(criteriaQuery.Factory.Dialect.LowercaseFunction)
            .Add("(")
            .Add(columnNames[0])
            .Add(")")
            .Add(" like ");
    }

    sqlBuilder.Add(criteriaQuery.NewQueryParameter(
        GetParameterTypedValue(criteria, criteriaQuery)).Single());

    return sqlBuilder.ToSqlString();
}

这意味着,如果我们使用Postgre方言 - ilike 。对于任何其他情况,我们应该看到(示例适用于SQL Server及其方言)

...
WHERE lower(this_.Name) like @p0'
,N'@p0 nvarchar(4000)'
,@p0=N'%country%'

同时检查主叫方QueryOverRestrictionBuilder.cs

/// <summary>
/// A case-insensitive "like", similar to Postgres "ilike" operator
/// </summary>
public TReturn IsInsensitiveLike(object value)