按未映射的参数过滤

时间:2014-04-21 14:43:26

标签: c# nhibernate icriteria

我如何声明参数(ids)并使用ICriteria API将其添加到NHibernate中select的部分?

DECLARE @ids xml ='<ScopeIds><id>417444AC-6C57-4CB7-91E1-6E0B5832EDBB</id></ScopeIds>'

SELECT * FROM MyTable p 
WHERE 
 /* other criterion list */ 
 AND @ids.exist('/ScopeIds[id=sql:column("ScopeId")]') = 1

1 个答案:

答案 0 :(得分:1)

此处的一种方式可能是SqlProjection

// the source xml snippet            
var xml = "<ScopeIds><id>417444AC-6C57-4CB7-91E1-6E0B5832EDBB</id></ScopeIds>";

// this SQL statement will represent the xml creation and call to '.exist'
var sql = " CAST('" + xml + "' AS xml)" +
          "     .exist('/ScopeIds[id=sql:column(\"ScopeId\")]') " +
          " AS idExists";

// here we declare the SQL Project, NHibernate how to manage low level sql
var projection = Projections.SqlProjection( sql
            , new string[] {"idExists"}
            , new IType[] {NHibernateUtil.Int32}
            );


// the criteria
var criteria = session.CreateCriteria<MyEntity>();

// and here we compare the above restriction if == 1 (is true)
criteria.Add(Restrictions.Eq(projection, 1));

// all other restrictions
...