开发者

NHibernate: Or-clause with Components

开发者 https://www.devze.com 2022-12-08 10:18 出处:网络
I have two classes: public class Person{ public virtual string Name { get; set;} public virtual Address Address { get; set; }

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(Expressi开发者_运维百科on.Eq("Name", "Xyz"))
.CreateCriteria("Address").Add(Expression.Eq("Street", "Xyz"));

What I want is and or-clause.

Thank you very much! Andy


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");
0

精彩评论

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