简体   繁体   中英

Group/Sort by Date Year/Month, Partial Select with NHibernate (Projection? Transformation?)

I'm building an ArchivesController for the open source asp.net MVC-3 blog platform FunnelWeb . We have an model called "Entry" which represents a blog entry which has a DateTime property called "Published" for when this entry was published. The purpose of the proposed ArchivesController is to create a wordpress-like archives link table that shows a descending list of all years and months for which we have posts with links to an archive index like '/archive/2011/9' and a count for the number of posts in the year/month.

ex:

  • December 2011 (2 posts)
  • November 2011 (4 posts)
  • October 2011 (1 post)

I'm not experienced with NHibernate and so wrote the initial query using like this:

public class GetArchiveDatesQuery : IQuery<ArchiveDate>
{
    public System.Collections.Generic.IEnumerable<ArchiveDate> Execute(ISession session, IDatabaseProvider databaseProvider)
    {
        var criteria = session.QueryOver<Entry>();

        var archiveDates = from entry in criteria.List<Entry>()
                                group entry by new { entry.Published.Year, entry.Published.Month } into entryGroup
                                orderby entryGroup.Key.Year descending, entryGroup.Key.Month descending
                                select new ArchiveDate()
                                {
                                    Year = entryGroup.Key.Year,
                                    Month = entryGroup.Key.Month,
                                    EntryCount = entryGroup.Count()
                                };

        return archiveDates;
    }
}

Where ArchiveDate is a new model I created to encapsulate the year-month-count information from this query.

This works, but I'd prefer to push the work off to SQL instead of doing the grouping and sorting in C#. I imagine on an active blog that has been around for several years with hundreds or thousands of posts would be much better off to do this in SQL so we don't return unnecessary data (like the entry content).

My question is how we can accomplish the above LINQ statement in an NHibernate fashion which results in the grouping/sorting occurring in SQL. I imagine it will involve some Criteria->Projection->Transformation sort of process.

The part I'm stuck on is accessing the month portion and year portion of the DateTime property for grouping and sorting which is currently accessed by the .Net DateTime object.

The blog engine is using NHibernate version 3.2.0.4000.

Update:

var query = from e in session.Query<Entry>()
            group e by new { e.Published.Year, e.Published.Month } into entryGroup
            select new
            {
                entryGroup.First().Published,
                EntryCount = entryGroup.Count()
            };

var archiveDates = from a in query.AsEnumerable()
                   orderby a.Published.Year descending, a.Published.Month descending
                   select new
                   {
                       Year = a.Published.Year,
                       Month = a.Published.Month,
                       EntryCount = a.EntryCount,
                   };

Original Answer:

could you try NHibernates LINQ-provider first and post the errors?

using NHibernate.Linq;


var archiveDates = from entry in session.Query<Entry>()
                   group entry by new { entry.Published.Year, entry.Published.Month } into entryGroup
                   orderby entryGroup.Key.Year descending, entryGroup.Key.Month descending
                   select new ArchiveDate
                   {
                       Year = entryGroup.Key.Year,
                       Month = entryGroup.Key.Month,
                       EntryCount = entryGroup.Count()
                   };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM