Net205
Net205

Reputation: 171

invoke sql function using nhibernate?

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!

Upvotes: 0

Views: 3444

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52725

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,%"));

Upvotes: 4

Related Questions