Yes you are right I could get it from the collection's size but it's obviously inefficient.
I'm not sure I want to create another value object for this though, but supposing I did, I don't really understand the code you gave me (sorry :( You have declared a property number_of_posts which is never used later. I think there's something wrong here :) On 13 mai, 15:02, Jason Meckley <jasonmeck...@gmail.com> wrote: > I would removed the countPosts property from the Source entity. If the > entity needs the count it can be retrieved from the post list > directly. > > class Source > { > IList<Post> Posts; > int countPosts { get {return Posts.Count; } } > > } > > however this would be inefficient for your current projection. For > that I would create a new value object > class SourceWithPostCount > { > Source source {get;set;} > int number_of_posts{get;set;} > > } > > and use a projection. Something like this. although I'm not 100% sure > how to group by an entity, if at all. i actually have a thread open > about this concept. > > var dtos = session > .CreateCriteria<Source>() > .SetProjection( > Projections.List() > .Add(Projections.GroupProperty("this")) > .Add(Projections.Count("Posts")) // or .Add > (Projections.Count(Projections.Property("Posts"))) > ) > .SetMaxResults(100) > .SetResultTransformer([can't remember the exact transformer]) > .List<SourceWithPostCount>(); > > On May 13, 9:20 am, graphicsxp <graphic...@googlemail.com> wrote: > > > Thanks for clarifying that ! The .List() in the doc was a bit > > confusing and it makes more sense with UniqueResult<int> as you said. > > > So in my example, how could I use aggregation to fill in the countPost > > property ? Is it possible at all, or can aggregate functions only be > > used for returning a value, as opposed to setting a property ? > > > On 13 mai, 13:37, Jason Meckley <jasonmeck...@gmail.com> wrote: > > > > 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 <graphic...@googlemail.com> 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 <zoidb...@googlemail.com> wrote: > > > > > > Try Projections. > > > > > >http://nhforge.org/doc/nh/en/index.html#querycriteria-projection > > > > > > On May 12, 3:56 pm, graphicsxp <graphic...@googlemail.com> 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 nhusers@googlegroups.com To unsubscribe from this group, send email to nhusers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---