开发者

How to create an ICriterion in NHibernate that filters based on the content of a collection

I have several ICriterion instances that I combine in various ways (conjunction, disjunction, etc.) based on user input. I'm having trouble creating an ICriterion that is based on matching a value in an associated collection.

For example, given a one-to-many relationship between Orders and OrderItems, I want to be able to create an ICriterion that selects all Orders that have an OrderItem with a Quantity > 100.

I've tried several things and haven't yet found anythin开发者_如何学Pythong that works.


i had a the same problem in a project. you need a pair of alias and criteria for each filtered collection.

KeyValuePair<string, ICriterion[]> collectionfilters = GetFromSomeWhere();

foreach (var association in collectionfilters)
{
    criteria.CreateAlias(association.Key, association.Key);

    foreach(var crit in association.Value)
    {
        criteria.Add(crit);
    }
}

// example
KeyValuePair<string, ICriterion[]> GetFromSomeWhere()
{
    return new KeyValuePair<string, ICriterion[]>("OrderItems", new []{ Restrictions.Gt("OrderItems.Quantity", 100) });
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜