NHibernate Criteria question

Posted by Jeneatte Jolie on Stack Overflow See other posts from Stack Overflow or by Jeneatte Jolie
Published on 2010-05-16T19:51:31Z Indexed on 2010/05/17 7:40 UTC
Read the original article Hit count: 285

Filed under:
|

I have a person object, which can have unlimited number of first names. So the first names are another object.

ie

person --- name
            --- name
            --- name

What I want to do is write an nhiberate query using which will get me a person who has certain names.

so one query might be find someone whose names are alison and jane and philippa, then next query may be one to find someone whose names are alison and jane.

I only want to return people who have all the names I'm search on. So far I've got

ICriteria criteria = session.CreateCriteria(typeof (Person));
criteria.CreateAlias("Names", "name");
ICriterion expression = null;
foreach (string name in namesToFind)
{
    if (expression == null)
    {
        expression = Expression.Like("name.Value", "%" + name + "%");
    }
    else
    {
        expression = Expression.Or(
            expression,
            Expression.Like("name.Value", "%" + name + "%"));
    }
}

if (expression != null)
    criteria.Add(expression);

But this is returning every person with ANY of the names I'm searching on, not ALL the names.

Can anyone help me out with this? Thanks!

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about icriteria