cs0815
cs0815

Reputation: 17388

select parent given child id

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

Answers (2)

cidico
cidico

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

Esteban Elverdin
Esteban Elverdin

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

Related Questions