【问题标题】:Filter by unmapped parameter按未映射的参数过滤
【发布时间】:2014-04-22 00:05:52
【问题描述】:

我如何声明一个参数(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

【问题讨论】:

    标签: c# nhibernate icriteria


    【解决方案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
    ...
    

    【讨论】:

    • 你为我节省了一夜不受打扰的睡眠,Radim)我一直坚持创建完整的SqlCriterion
    • 钢 我有一种做错事的感觉——我真正需要的是创建“IN (Very_Large_List_Of_Guids)”标准。有什么建议吗?
    • 如果可能,for IN 语句最好只使用IN 子句 (Restrictions.In()),并传递一组搜索到的 GUIDS。如果这个数组太大,说明需求有问题。我更愿意将它们放入数据库中,然后使用Subquery。子查询语法示例stackoverflow.com/questions/16621961
    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多