Hej Chris,

Thank you for taking the time to answer.

I was thinking about projections, but wasnt really sure how to apply
it in this case.

I tried your approach:
var query = _nHibernateSession.CreateCriteria<Contract>()
                .Add<Contract>(c => c.Unit.Id == unitId)
                .Add<Contract>(c => c.StatusId > 7)
                .Add<Contract>(c => c.StopDate > DateTime.Now)
                .SetProjection(
                        LambdaProjection.Property<Contract>(c => c.Categories)
                        );

This doesnt return Contracts, but neither does it return any
categories. Tried with aliases as well.

Taavi


On 20 aug, 19:58, Chris J <[email protected]> wrote:
> Does it work query the relationships from the other direction? I tried
> building a similar query with my own domain model, but the
> relationships weren't quite the same. Here's what I was thinking, not
> sure if it will work. This is with lambda extensions, but without
> creating aliases.
>
> var query = session.CreateCriteria<Contract>()
>         .Add<Contract>(c => c.Unit.Id == unitId)
>         .SetProjection(LambdaProjection.Property<Contract>(c =>
> c.Categories));
>
> On Aug 19, 3:04 pm, Melborp <[email protected]> wrote:
>
>
>
> > The subject is confusing, i couldnt figure out a way to ask it in
> > clear english. I do have a sample code which will clear it up.
>
> > Imagine having three entities:
> > Category
> > Contract
> > Unit
>
> > Contract and Category have a many to many relationship.
> > Contract also has one-to-one with Unit.
>
> > My interest is to get all categories that specific unit contracts
> > have. A Criteria could be described as next:
>
> > var query = _nHibernateSession.CreateCriteria<Category>("cat")
> > .CreateAlias("cat.Contracts", "contract")
> > .Add(Expression.Eq("contract.Unit.Id", unitId));
>
> > or with lambda extensions:
>
> > Category categoryAlias = null;
> > Contract contractAlias = null;
>
> > var query = _nHibernateSession.CreateCriteria(typeof (Category), () =>
> > categoryAlias)
> >                 .CreateAlias(() => categoryAlias.Contracts, () => 
> > contractAlias)
> >                 .Add(() => contractAlias.Unit.Id == unitId);
>
> > This however seems to have a sideeffect of loading in all the
> > referencing Contracts as well. I only want the categories loaded in.
> > Contract is a very complex entity and is completely unnessecary in the
> > current context.
>
> > Can this be done with Criteria API?
> > I tried to SetFetchMode  to Lazy for category.Contracts but that had
> > no effect. The SQL query generated selects contracts with each
> > category.
>
> > This is however the only way i could query for categories with the
> > condition needed. Am i missing something?
> > I prefer a no magic string solution and thats also why i have two
> > different query representations that are working. I must admit that
> > the non lambda is more readable. I wish Nhib to Linq could do this in
> > 2.1.2.- Peida kommenteeritud tekst -
>
> - Näita kommenteeritud teksti -

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to