3

I want to excute a query using nhibernate, but I want to add parameters as inline string without aliases, the following examples will explain what I mean.

Standard generated query:

SELECT * FROM Accounts WHERE AccountNumber in (@p0 , @p1 , @p2)

Intended query:

SELECT * FROM Accounts WHERE AccountNumber in ('0','1','2')

FYI: I'm using Orchard CMS

mdameer
  • 1,540
  • 2
  • 14
  • 17
  • Can you rephrase the question please? Do you mean that you don't want parameterized queries? – Vijay Gill Jul 13 '16 at 12:46
  • Yes, you are right. I want to use the standard way to build the query in nhibernate but without parameters in the generated query (inline parameters), if there is something like settings for query to achieve it? – mdameer Jul 13 '16 at 12:53

3 Answers3

1

Finally, I found the solution using Expression.Sql, which used to build hybrid query as lambda expressions and plain text expressions, and the following code will explain it:

var accountQuery = _session.CreateCriteria(typeof(AccountRecord));
accountQuery.Add(Expression.Sql("AccountNumber in ('" + string.Join("','", contractAccountsList) + "')"));
accountQuery.Add(Restrictions.Eq("IsRegistered", true));
var result = accountQuery.List().AsEnumerable();

You can build it using Criteria or QueryOver as you want.

Thanks for everyone tried to help me solving this problem.

mdameer
  • 1,540
  • 2
  • 14
  • 17
0

Can use HQL:

var accountNumbers = new[] {1, 2, 3, 4};

var hql = string.Format("from Account where Account.AccountNumber in ({0})", string.Join(",",accountNumbers)); 

var results = session.CreateQuery(hql).List<Account>();
mxmissile
  • 11,464
  • 3
  • 53
  • 79
0

Imho, looking in the NHibernate reference there is no such thing as disabling parameter generation.

I would go with generating the query without execution, then replacing the parameters manually, then execute it.

Community
  • 1
  • 1
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58