开发者

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

开发者 https://www.devze.com 2022-12-29 15:19 出处:网络
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 i开发者_如何学Gonstance of the class Foo, but I\'m unsure how I would do this for a IList<

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 i开发者_如何学Gonstance of the class Foo, but I'm unsure how I would do this for a IList<Foo>.

So for a single instance of the class Foo, this works:

        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?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号