我可以自定义nHibernate中查询结果的排序规则吗?

我可以自定义nHibernate中查询结果的排序规则吗?,nhibernate,Nhibernate,在普通的旧SQL中,我可以执行以下操作: 从mytable COLLATE Latin1\u General\u CS\u AS中选择* 有没有一种方法可以指定我希望在nHibernate、HQL或criteria中为给定查询使用的排序规则类型 German Schuager已经成功地在运行时指定了排序规则。看一看 var user=session.CreateCriteria(typeof(user)) .Add(Expression.Sql(“类似用户名的?将现代西班牙语与CS作为对照”,用

在普通的旧SQL中,我可以执行以下操作:

从mytable COLLATE Latin1\u General\u CS\u AS中选择*


有没有一种方法可以指定我希望在nHibernate、HQL或criteria中为给定查询使用的排序规则类型

German Schuager已经成功地在运行时指定了排序规则。看一看

var user=session.CreateCriteria(typeof(user))
.Add(Expression.Sql(“类似用户名的?将现代西班牙语与CS作为对照”,用户名,NHibernateUtil.String))
.UniqueResult();
Shuager还提供了一种定义自定义函数以执行类似操作的方法。这也具有在HQL中可用的优点

他的自定义函数实现对于您的问题和我自己的需求来说太具体了,因此以下是我结束时的实现:

/// <summary>
/// Customized dialect for allowing changing collation on <c>like</c> statements.
/// </summary>
public class CustomMsSqlDialect : MsSql2008Dialect
{
    /// <summary>
    /// Default constructor.
    /// </summary>
    public CustomMsSqlDialect()
    {
        RegisterFunction("withcollation",
            new WithCollationFunction());
    }
}

/// <summary>
/// Add collation to string argument.
/// </summary>
[Serializable]
public class WithCollationFunction : SQLFunctionTemplate, IFunctionGrammar
{
    /// <summary>
    /// Default constructor.
    /// </summary>
    public WithCollationFunction()
        : base(NHibernateUtil.String, "?1 collate ?2")
    {
    }

    bool IFunctionGrammar.IsSeparator(string token)
    {
        return false;
    }

    bool IFunctionGrammar.IsKnownArgument(string token)
    {
        return Regex.IsMatch(token, "[A-Z][A-Z0-9_]+_(?:CS|CI)_(?:AS|AI)(?:_KS)?(?:_WS)?", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
    }
}
自定义排序

from Cat as c
where c.Name like withCollation('fel%', French_CI_AI)

适用于Nhib 3.2。

最初的响应以奇怪的语法结尾:“where customLike(c.Name,'fel%',CI_AI)”。我已经改变了这一点(在我自己的代码中)以获得更自然、更灵活的语法。
from Cat as c
where c.Name like 'fel%'
from Cat as c
where c.Name like withCollation('fel%', French_CI_AI)