invoke sql function using nhibernate?

2020-05-08 21:03发布

I want to query like this:

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

so, I wrote this code:

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

I get sql similar :

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

But it is not what I want,how to do it??? thanks!

1条回答
smile是对你的礼貌
2楼-- · 2020-05-08 21:19

You were in the right track, but you forgot to add what you wanted to concat.

Try this:

cri.Add(Restrictions.Like(
            Projections.SqlFunction("concat",
                                    NHibernateUtil.String,
                                    Projections.Constant(","), 
                                    Projections.Property("ServiceCodes"),
                                    Projections.Constant(",")),
        "%,33,%"));
查看更多
登录 后发表回答