使用QueryOver API进行复杂或查询

时间:2011-06-16 11:50:56

标签: nhibernate

我有一个(简化的)类层次结构,例如:

public class Order
{
    public Customer Customer { get; set; }
    public Address Address { get; set; }
}
public class Customer
{
    public Address Address { get; set; }
}
public class Address
{
    public string CompanyName { get; set; }
    public string City { get; set; }
    public string Zip { get; set; }
}

现在我需要查询在地址的CompanyName,City或Zip上匹配的订单 - 但是订单或关联的客户。

我已经达到了:

public IList<Order> FindOrders(string search, ISession session)
{
    var orders = session.QueryOver<Order>()
          .Where(o => o.Address.City == search || o.Address.Zip == search || o.CompanyName == search).List();
}

如何加入客户?

1 个答案:

答案 0 :(得分:2)

您的意思是客户地址吗?如果是这样,我认为你正在寻找这样的东西:

public IList<Order> FindOrders(string search, ISession session)
{
    var orders = session.QueryOver<Order>(() => orderAlias)
          .JoinAlias(() => orderAlias.Customer, () => customerAlias)
          .Where(() => orderAlias.Address.City == search || orderAlias.Address.Zip == search || orderAlias.CompanyName == search)
          .Or(() => customerAlias.Address.City == search || customerAlias.Address.Zip == search || customerAlias.CompanyName == search)
          .List();
}

如果你发现你写了很多这样的问题,你可能会考虑做一些全文搜索来让你的生活更轻松(我推荐lucene.net,可能是通过NHibernate.Search)