Geeks With Blogs
Bob Palmer's Developer Blog .NET, SQL, and Silverlight Development

One of my current side projects is my wife's archaeology site.  For her forums, we decided to go with a 'tag' system, where each message has multiple tags, and clicking on a tag link finds other messages with that same tag. 

In the sample below, I find all Topics who have at least one Tag in their Tags bag matching the passed TagID.

Since a user could potentially start a search with no selected tag, passing a zero value will bypass the tag search, and just return the most recent posts (as determined by the startindex and pagesize used for pagination).

Enjoy!

        public IList<TopicHeader> GetPageByTag(int StartIndex, int PageSize, int TagID)
        {
            List<TopicHeader> topiclist = new List<TopicHeader>();
           
            using (var session = AFWConfiguration.SessionFactory.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    ICriteria q = session.CreateCriteria(typeof (TopicHeader))

                        .AddOrder(Order.Desc("TopicTime"));
                    if (TagID > 0)
                    {
                        q = q.CreateCriteria("Tags")
                             .Add(Expression.Eq("TagID", TagID));
                    }
                    q = q.SetFirstResult(StartIndex).SetMaxResults(PageSize);
                    topiclist = q.List<TopicHeader>() as List<TopicHeader>;
                }
            }
            return topiclist;
        }

Posted on Sunday, November 29, 2009 3:10 PM | Back to top


Comments on this post: Checking for a single value in an item's bag in NHibernate

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © BobPalmer | Powered by: GeeksWithBlogs.net