本文介绍了NHibernate支持功能:LEN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想得到结果:
select * from Cate where LEN(code)=2
我的代码:
var filter1 = Restrictions.Eq(
Projections.SqlFunction("LEN", NHibernateUtil.Int32,
Projections.Property("code")), 2);
var query =
repository.Session.QueryOver<Cate>().Where(filter1).List();
Assert.IsTrue(query.Count > 0);
但是,出现了错误:
如何在Nhibernate中使用Len函数的SQLServer2008?
How can I use in Nhibernate the SQLServer2008 of the Len function?
推荐答案
您应该在Configuration类中注册您的函数
You should register your function either in the Configuration class
config.AddSqlFunction("len", new StandardSafeSQLFunction("len", NHibernateUtil.Int32, 1));
或创建自定义方言
public class MyDialect : MsSql2008Dialect
{
public MyDialect()
{
RegisterFunction("len", new StandardSafeSQLFunction("len", NHibernateUtil.Int32, 1));
}
}
这篇关于NHibernate支持功能:LEN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!