How do I query many-to-many relationship in NHibernate
For instance, in this example Employee and Address has a many-to-many relationship, and an Employee
class has a property
public virtual IList<Address> Addresses { get; set; }
In this example, how would I create a linq query to get all Employee that has X
as one of its address (X is a Address
variable)?
I ca开发者_开发知识库n think of:
q => q.Where(employee => employee.Addresses.IndexOf(X)>=0);
But I think this will not be optimised and will be very costly.
Try this:
q => q.Where(e => e.Addresses.Any(<your condition goes here>))
for example
q => q.Where(e => e.Addresses.Any(a => a.City == "Moscow"))
精彩评论