Question

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)?

Was it helpful?

Solution

I assume you will have access to SomeCategory list, then try

var category = someCategoryList.FirstOrDefault(e => e.SomeInstances
                               .Any(a => a.Id == someclassId));

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top