Complex or-query using QueryOver API
I have a (simplified) class hierarchy such as this:
public class Order
{
public Customer Customer { get; set; }
public Address Address { get; set; }
}
public class Custom开发者_如何学JAVAer
{
public Address Address { get; set; }
}
public class Address
{
public string CompanyName { get; set; }
public string City { get; set; }
public string Zip { get; set; }
}
Now I need to query Orders that match on either CompanyName, City or Zip of the Address - but on either the Order or the associated Customer.
I have gotten as far as:
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();
}
How do I include the customer?
Do you mean the customer address? If so, I think you're looking for something like this:
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();
}
If you find you're writing a lot of queries like this, you may want to consider doing some kind of fulltext search to make your life easier (I'd recommend lucene.net, possibly via NHibernate.Search)
精彩评论