如何在nHibernate中设置Criteria以查询多个值

时间:2010-05-06 09:53:44

标签: .net nhibernate criteria

我想根据列表的内容查询一组结果,我已经设法为类Foo的单个实例执行此操作,但我不确定如何执行此操作对于IList<Foo>

因此对于类Foo的单个实例,这有效:

        public ICriteria CreateCriteria(Foo foo)
        {
            return session
                .CreateCriteria<Component>()
                .CreateCriteria("Versions")
                .CreateCriteria("PublishedEvents")
                .Add(Restrictions.And(Restrictions.InsensitiveLike("Name", foo.Name, MatchMode.Anywhere),
                                      Restrictions.InsensitiveLike("Type", foo.Type, MatchMode.Anywhere)))
                .SetCacheable(true);
        }

但是,当method参数是Foo

的列表时,我该怎么做呢?
 public ICriteria CreateCriteria(IList<Foo> foos)
    {
        return session
            .CreateCriteria<Component>()
            .CreateCriteria("Versions")
            .CreateCriteria("PublishedEvents")
            .Add(Restrictions.And(Restrictions.InsensitiveLike("Name", foo.Name, MatchMode.Anywhere),
                                  Restrictions.InsensitiveLike("Type", foo.Type, MatchMode.Anywhere)))
            .SetCacheable(true);
    }

1 个答案:

答案 0 :(得分:1)

如果您正在考虑要对此查询执行的操作,那么在您正在使用的构造中查询它实际上没有意义。您真正拥有的唯一选择是循环并动态创建标准,如下所示:

 public ICriteria CreateCriteria(IList<Foo> foos)
    {
        var criteria = session
            .CreateCriteria<Component>()
            .CreateCriteria("Versions")
            .CreateCriteria("PublishedEvents")
            .SetCacheable(true);

        foreach(var foo in foos)
        {
            criteria.Add(Restrictions.And(Restrictions.InsensitiveLike("Name", foo.Name, MatchMode.Anywhere),Restrictions.InsensitiveLike("Type", foo.Type, MatchMode.Anywhere)));
        }
        return criteria;
    }