简体   繁体   English

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

[英]How do I setup a Criteria in nHibernate to query against multiple values

I want to query for a set of results based on the contents of a list, I've managed to do this for a single instance of the class Foo , but I'm unsure how I would do this for a IList<Foo> . 我想基于列表的内容查询一组结果,我已经设法对Foo类的单个实例进行了此操作,但是我不确定如何对IList<Foo>

So for a single instance of the class Foo , this works: 因此,对于类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);
        }

But how do I do this when the method parameter is a list of Foo ? 但是,当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);
    }

If you're thinking about what you're trying to do with this query, it doesn't actually make sense to query it in the construct you're using. 如果您正在考虑要对此查询执行的操作,则在您使用的构造中对其进行查询实际上没有任何意义。 The only option you really have is to loop through and dynamically create the criteria like so: 您真正拥有的唯一选择是遍历并动态创建条件,如下所示:

 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM