Reputation: 17388
A seemingly simple problem turns out more difficult than I thought:
public class SomeCategory
{
public virtual int Id { get; set; }
public virtual IList<SomeClass> SomeInstances { get; set; }
}
public class SomeClass
{
public virtual int Id { get; set; }
}
There is a 1:m relationship between SomeClass and SomeCategory (i.e. an instance of SomeClass belongs to exactly one SomeCategory and SomeCategory can have several SomeClass instances).
Question how do I get the SomeCategory given a SomeClass Id (Linq to NHibernate)?
Upvotes: 0
Views: 661
Reputation: 386
You can also do it using QueryOver.
Parent parentAlias = null;
Child childAlias = null;
var query = session.QueryOver<Parent>(() => parentAlias)
.JoinAlias(()=> parent.Childs, ()=> childAlias)
.Where(()=> childAlias.Parent.Id == id)
.Select(()=> childAlias.Parent)
.SingleOrDefault();
Upvotes: 1
Reputation: 3582
I assume you will have access to SomeCategory
list, then try
var category = someCategoryList.FirstOrDefault(e => e.SomeInstances
.Any(a => a.Id == someclassId));
Upvotes: 3