문제

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

도움이 되었습니까?

해결책

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

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

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top