使用nhibernate调用sql函数?

时间:2010-04-21 03:29:53

标签: sql nhibernate

我想这样查询:

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,%';

但这不是我想要的,该怎么办? 谢谢!

1 个答案:

答案 0 :(得分:4)

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

试试这个:

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