Domanda

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

È stato utile?

Soluzione

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

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

Altri suggerimenti

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top