Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:31:49+00:00 2026-06-12T22:31:49+00:00

Here is my domain: public class ForumTheme { public virtual String Name { get;set;

  • 0

Here is my domain:

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; }
}

What i want to get is:

The list of ForumThemes with top 5 of most recent ForumMessages for each ForumTheme

Is it possible to do that in NHibernate ?

UPDATE

Here is another possible scenario.

The domain

The domain describes a typical hierarchical blog/news site.

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; }
}

So one Channel has many Categories in it and one Category has many Articles.

What i want to do here is to grab TOP N Articles for each Channel (ignoring the fact that they come from different categories). The articles will be shown on the landing page of my portal.

In the application i use ViewModels which hold only subset of actual database data.
Here is my composite ViewModel

The DTOs

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; }
    }
}

The query to get the channels is quite straightforward (we show only those channels which have active articles in it):

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();

The query above gives me the list of Channels with active articles. Now i will get the articles themselves:

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();
}

But the 2 queries above are subject to SELECT + 1 problem. I feel that using futures i can do it using lesser round trips. I’m not so familiar with NHibernate yet.
Is it possible to get everything with one roundtrip and hydrate DTOs? Using something like ROW_NUMBER() OVER (PARTITION BY….) with QueryOver? I don’t want to use HQL or raw SQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:31:50+00:00Added an answer on June 12, 2026 at 10:31 pm

    using 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;
    

    for your second scenario

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the domain classes I'm working with: class Foo { String name, type static
I have a domain class like this: public class DomainClass { public virtual string
Here are three classes in my domain: public class Quote : IEntity, IAggregateRoot {
Here is the domain that I wish to have: public class Person { public
Let's assume I have the following domain: public class Movie { public string Id
Here is my simplified code: // Domain models public class Order { public int
If I have the following domain object: public class Customer { public virtual Guid
Here are the domain model classes: public abstract class BaseClass { ... } public
Take this POJO as the domain. public class Invoice { private String codeNumber; private
So here is my problem. I want to redirect name.domain.com/trips/1 to domain.com?username=name&trip=1 using modrewrite.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.