2010-04-21 39 views
0

我想这样的查询:使用nhibernate调用sql函数?

select * from table where concat(',', ServiceCodes, ',') like '%,33,%'; 
select * from table where (','||ServiceCodes||',') like '%,33,%'; 

所以,我写了这个代码:

ICriteria cri = NHibernateSessionReader.CreateCriteria(typeof(ConfigTemplateList)); 
cri.Add(Restrictions.Like(Projections.SqlFunction("concat", NHibernateUtil.String, Projections.Property("ServiceCodes")), "%,33,%")); 

我得到的SQL类似:

select * from table where (ServiceCodes) like '%,33,%'; 

但它不是我想要的,怎么做??? 谢谢!

回答

4

你在正确的轨道,但你忘了添加你想要的连接。

试试这个:

cri.Add(Restrictions.Like(
      Projections.SqlFunction("concat", 
            NHibernateUtil.String, 
            Projections.Constant(","), 
            Projections.Property("ServiceCodes"), 
            Projections.Constant(",")), 
     "%,33,%"));