Hej, I have been trying numerous versions of queries with projections. Hasnt worked yet. I getting a feeling Projections deosnt work with collections?
Taavi On Aug 21, 10:35 am, Melborp <[email protected]> wrote: > I should have added the result before. The query looked like: > > SELECT this_.contract_id as y0_ > FROM [Contract] this_ > inner join contract_category categories3_ > on this_.contract_id = categories3_.contract_id > inner join [Category] categoryal1_ > on categories3_.category_id = categoryal1_.cat_id > WHERE this_.unit_id = 8321 /* @p0 */ > and this_.status_contract_id > 7 /* @p1 */ > and this_.stop_date > '2010-08-21T10:33:15.00' /* @p2 */ > > The result as you can read is the list of contract id-s. > > On 20 aug, 23:25, Chris J <[email protected]> wrote: > > > > > What does it return? > > > On Aug 20, 1:09 pm, Melborp <[email protected]> wrote: > > > > 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 -- Peida kommenteeritud tekst - > > > - Näita kommenteeritud teksti -- Hide quoted text - > > - Show quoted text - -- 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.
