Search code examples
c#.netnhibernatequeryover

SQLCriterion ArgumentOutOfRangeException


What is the correct syntax to create a SQLCriterion?

I have the following code:

var sqlCriterion = new SQLCriterion(
                new SqlString("{alias}.Id IN (SELECT Id FROM dbo.fGetSomeIds(?1, ?2))"),
                new object[] { "param1", "param2" },
                new IType[] { NHibernateUtil.String, NHibernateUtil.String });

query.Where(sqlCriterion);

where query is my QueryOver-instance (created with NHibernateSession)

When I call query.List() I get the following exception:

Index was out of range. Must be non-negative and less than the size of the collection parameter name:index 

which is thrown somewhere in NHibernate.Criterion.SQLCriterion.ToSqlString(..)

Is the syntax of my SQLCriterion-constructor wrong or am I missing something else?


Solution

  • This adjustment should make it:

    var criterion = NHibernate.Criterion.Expression
        .Sql("({alias}.Id IN (SELECT Id FROM dbo.fGetSomeIds(?, ?))"
            + " AS MyCriteria",
            new object[] { "param1", "param2" },
            new IType[] { NHibernateUtil.String, NHibernateUtil.String });
    
    // query.Where(sqlCriterion);
    query.Where(criterion);