the example above will return the number of cats that are black. it's similar to the sql select count(*) from cat where color = 'black' in the sample projection you could change List() to UniqueResult<int> ()
here is the difference between count and distinct count Sample Data: ID 1 1 2 3 4 4 4 Count() will return 7 (1, 1, 2, 3, 4, 4, 4) DistinctCount() will return 4 (1, 2, 3, 4) On May 13, 5:01 am, graphicsxp <[email protected]> wrote: > Thanks zoid, > I actually had a look already at Projections but I don't really > understand how they work. For instance in the documentation : > > IList results = session.CreateCriteria(typeof(Cat)) > .SetProjection( Projections.RowCount() ) > .Add( Expression.Eq("Color", Color.BLACK) ) > .List(); > > There's no explanations about what this is supposed to return.... > > That said, even though I don't understand it, I have a feeling this is > not the correct projection for my problem. There is a Count() and > DistinctCount(), but again, I have no idea how to use them and there's > very little doc about this. > > On 12 mai, 23:18, zoid <[email protected]> wrote: > > > Try Projections. > > >http://nhforge.org/doc/nh/en/index.html#querycriteria-projection > > > On May 12, 3:56 pm, graphicsxp <[email protected]> wrote: > > > > Hello, > > > > I'd like to get some help with a HQL syntax. Here are my classes : > > > > class Post > > > { > > > Source theSource ; > > > > } > > > > class Source > > > { > > > IList<Post> Posts; > > > > } > > > > In the mapping of Source I have : > > > > <bag name="Posts" lazy="true"> > > > <key column="SourceID" /> > > > <one-to-many class="Post"/> > > > </bag> > > > > and in the mapping of Post I have : > > > > <many-to-one class="Source" name="theSource" cascade="all-delete- > > > orphan" column="SourceID" /> > > > > Now what I want is to get a list of all the source entities that are > > > used by all the post entities created in a given date range : > > > > ICriteria sourceCriteria = SessionManager.GetCurrentSession > > > ().CreateCriteria(typeof (Source)); > > > > sourceCriteria.CreateCriteria("Posts") > > > .Add(Restrictions.Lt("DateCreated", DateTime.Parse(parameter.Attribute > > > ("enddate").Value))) > > > .Add(Restrictions.Gt("DateCreated", DateTime.Parse(parameter.Attribute > > > ("startdate").Value))); > > > > //make sure we get distinct Source > > > sourceCriteria.SetResultTransformer(new > > > DistinctRootEntityResultTransformer()); > > > > return sourceCriteria.SetMaxResults(100).List<Source>(); > > > > That works fine. Now let's change the Source class slightly : > > > > class Source > > > { > > > IList<Post> Posts; > > > int countPosts; > > > > } > > > > I still don't want to load the Posts list (you've noticed that I used > > > lazy-loading), however I would like my query to fill in the countPosts > > > property with the number of Posts using each publication. > > > > How do I do that using the above HQL ? > > > > Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
