NHibernate supports the function: LEN

I want to get the results:

select * from Cate where LEN(code)=2 

My code is:

 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); 

However, there were errors:

NHibernate.HibernateException: Current dialect of NHibernate.Dialect.MsSql2008Dialect does not support function: LEN

How can I use Len functions in Nhibernate SQLServer2008?

+1
source share
2 answers

You must register your function either in the configuration class

 config.AddSqlFunction("len", new StandardSafeSQLFunction("len", NHibernateUtil.Int32, 1)); 

or to create a custom dialect

 public class MyDialect : MsSql2008Dialect { public MyDialect() { RegisterFunction("len", new StandardSafeSQLFunction("len", NHibernateUtil.Int32, 1)); } } 
-2
source

Use length instead of len . The way it registered in the dialect.

+5
source

Source: https://habr.com/ru/post/1499378/


All Articles