NHibernate TOP N Rows Per Group

时间:2012-10-05 13:42:34

标签: c# nhibernate queryover

这是我的域名:

public class ForumTheme
{
    public virtual String Name { get;set; }
}

public class ForumTopic
{
    public virtual IList<ForumTheme> Themes { get;set; }
}

public class ForumMessage
{
    public virtual IList<ForumTopic> Topics { get;set; }

    public virtual DateTime DatePosted { get; set; }
}

我想得到的是:

论坛主题列表,每个论坛主题的最新论坛消息中排名前5位

是否有可能在NHibernate中做到这一点?

更新

这是另一种可能的情况。

域名

该域描述了典型的分层博客/新闻网站。

public class Channel
{
    public virtual Int32 Id { get; set; }
    public virtual String Name { get; set; }

    public virtual IList<Category> Categories { get; set; }
}

public class Category
{
    public virtual IList<Article> Articles { get; set; }
}

public class Article
{
    public virtual String Name { get; set; }
    public virtual DateTime PublishDate { get; set; }
    public virtual Boolean IsActive { get; set; }
}

因此,一个频道有很多类别,一个类别有很多文章。

我想在这里做的是为每个频道抓取TOP N文章(忽略它们来自不同类别的事实)。这些文章将显示在我的门户网站的着陆页上。

在应用程序中,我使用ViewModels,它只包含实际数据库数据的子集。 这是我的复合ViewModel

DTO

public class ChannelDto
{
    public Int32 Id { get; set; }

    public String Name { get; set; }

    public List<ArticleDto> Articles { get; set; }

    public class ArticleDto
    {
        public String Name { get; set; }
    }
}

获取频道的查询非常简单(我们只显示那些包含活动文章的频道):

Category categoryAlias = null;
Article articleAlias = null;
ChannelDto.ArticleDto articleDtoAlias = null;

List<ChannelDto> channels = _session.QueryOver<Channel>()
                                    .Inner.JoinAlias(x => x.Categories, () => categoryAlias)
                                    .Inner.JoinAlias(x => categoryAlias.Articles, () => articleAlias)
                                    .Where(x => articleAlias.IsActive)
                                    .SelectList(list => list
                                        .Select(x => x.Id)
                                        .Select(x => x.Name)
                                    )
                                    .List<Object[]>
                                    .Select(x => new ChannelDto
                                    {
                                        Id = (Int32) x[0],
                                        Name = (String) x[1]
                                    })
                                    .ToList();

上面的查询为我提供了包含有效文章的频道列表。现在我将自己得到文章:

foreach(var channel in channels)
{
    channel.Articles = _session.QueryOver<Channel>()
                               .Inner.JoinAlias(x => x.Categories, () => categoryAlias)
                               .Inner.JoinAlias(x => categoryAlias.Articles, () => articleAlias)
                               .Where(x => x.Id == channel.Id)
                               .OrderBy(x => articleAlias.PublishDate).Desc
                               .SelectList(list => list
                                   .Select(x => articleAlias.Name).WithAlias(() => articleDtoAlias.Name)
                               )
                               .Take(5)
                               .List<Object[]>
                               .Select(x => new ChannelDto.ArticleDto
                               {
                                   Name = (String) x[0]
                               })
                               .ToList();
}

但上面的2个查询受SELECT + 1问题的影响。我觉得使用期货我可以用较少的往返来做。我对NHibernate还不太熟悉。 是否可以通过一次往返和水合DTO来获得所有东西?在QueryOver中使用ROW_NUMBER()OVER(PARTITION BY ....)之类的东西?我不想使用HQL或原始SQL。

1 个答案:

答案 0 :(得分:3)

使用2个rountrips

var themes = session.Query<ForumTheme>().Select(t => new ThemeDto(t.Name)).List();

var topMessages = new List<IEnumerable<string>>(themse.Count);

foreach(var theme in themes)
{
    var query = from message in session.Query<ForumMessage>()
                from topic in message.Topics
                from t in topic.Themes
                where t.Name == theme.Name
                orderby message.DatePosted desc
                select message;
    topMessages.Add(query.Take(5).ToFuture());
}

for(int i = 0;i < topMessages.Count; i++)
{
    themes.TopMessages = topMessages[i].ToList();
}

return themes;

第二个场景

foreach(var channel in channels)
{
    channel.Articles = _session
        .QueryOver<Channel>()
        .Inner.JoinAlias(x => x.Categories, () => categoryAlias)
        .Inner.JoinAlias(x => categoryAlias.Articles, () => articleAlias)
        .Where(x => x.Id == channel.Id)
        .OrderBy(x => articleAlias.PublishDate).Desc
        .SelectList(list => list
            .Select(x => articleAlias.Name).WithAlias(() => articleDtoAlias.Name)
        )
        .TransformUsing(Transformers.AliasToBean<ChannelDto.ArticleDto>());
}

// execute all subqueries at once through iterating one
channels[0].Articles.Any();