NHibernate Filter基于List <t>

时间:2016-06-02 20:28:04

标签: nhibernate

我正在使用NHNetnet 3.1 for .Net,我有这种情况。

ClassA.cs

public class ClassA
{
    public virtual string ID_1 { get; set; }
    public virtual string ID_2 { get; set; }

    public virtual string Nome { get; set; }
}

ClassA.hbm.xml

  <class name="ClassA" table="TableA" lazy="false">
    <composite-id>
      <key-property name="ID_1" column="ID_1" />
      <key-property name="ID_2" column="ID_2" />
    </composite-id>

    <property name="Nome">
      <column name="Nome" sql-type="varchar" not-null="false" />
    </property>
  </class>

我有一个列表List<ClassA>,我需要根据此列表过滤TableA,我该怎么做?

List ClassA
| 1 | 1 | Teste1 |
| 2 | 3 | Teste3 |

TableA
| 1 | 1 | Teste1 |
| 2 | 2 | Teste2 |
| 2 | 3 | Teste3 |
| 2 | 4 | Teste4 |

Result must be
| 1 | 1 | Teste1 |
| 2 | 3 | Teste3 |

2 个答案:

答案 0 :(得分:0)

所以

var classAList = List<ClassA> { new ClassA { ... }, ....};

var containsClassAList = mySession.Query<ClassA>().Where(c => classAList.Contains(c));
这样的事情。以上是伪代码。我现在面前没有一个nhibernate项目。但我之前使用过包含。您也可以尝试All();

答案 1 :(得分:0)

好吧,我用Criteria获得了另一个代码,

Disjunction ConjuntoOr = Restrictions.Disjunction();

foreach (ClassA item in itens)
{ 
   ConjuntoOr.Add
   (
      Expression.Conjunction()
         .Add(Restrictions.Eq("ID_1", item.ID_1))
         .Add(Restrictions.Eq("ID_2", item.ID_2))
   );
}

ICriteria criteria =
   mySession
      .CreateCriteria<ClassA>()
      .Add(ConjuntoOr)
      .List<ClassA>();

由NHibernate创建的查询

NHibernate: 
SELECT 
    this_.ID_1 as COD1_6_0_, 
    this_.ID_2 as COD2_6_0_,
    this_.Nome as NOME3_6_0_
FROM 
    dbo.TableA this_ 
WHERE 
    (
        (this_.ID_1 = @p0 and this_.ID_2 = @p1) or 
        (this_.ID_1 = @p2 and this_.ID_2 = @p3)
    );

@p0 = '1' [Type: String (4000)], @p1 = '1' [Type: String (4000)], 
@p2 = '2' [Type: String (4000)], @p3 = '3' [Type: String (4000)]

这正是我所寻找的。 this post

的Thx