tags:

views:

40

answers:

2

Hello!

I have two classes:

public class Person{

  public virtual string Name { get; set;}
  public virtual Address Address { get; set; }
}

public class Address{
  public virtual string Street { get; set; }
}

Now I want NHibernate to give me all Persons where Name is equal to "Xyz" or Address.Street is equal to "Xyz".

This approach is an and-conjunction:

ICriteria criteria = session.CreateCriteria(typeof(Person));
criteria.Add(Expression.Eq("Name", "Xyz"))
.CreateCriteria("Address").Add(Expression.Eq("Street", "Xyz"));

What I want is and or-clause.

Thank you very much! Andy

A: 

I've always just used HQL queries for this - much easier to write and read (at least if you're used to SQL. It's something like this:

IQuery query = session.CreateQuery("FROM Person p WHERE p.Name = :Name OR p.Address.Street = :Street");
query.SetParameter("Name", "Xyz");
query.SetParameter("Street", "Xyz");
Evgeny
Perfect! Thank you very much. You're right, these HQL queries are much easier :-)
Marlene
A: 

I tried this, but for some parameters I set, I get hundreds of results. And they are all of the same dataset. What could it be?

Thank you!

Marlene