Solo tire de Top X usando Fluent NHibernate HasMany

I have the following mapping in Fluent-NHibernate Map

public class PostMap : ClassMap<Post>
{
    public PostMap()
    {
        Id(i => i.Id).GeneratedBy.GuidComb();
        Map(x => x.SiteId);
        Map(x => x.Message);
        Map(x => x.DateCreated);
        Map(x => x.DateModified);
        HasMany(x => x.Comments)
            .OrderBy("DateCreated DESC")
            .ReadOnly()
            .Not.LazyLoad();
    }
}

There are a lot of comments on each post, what I would like to do is only bring back the top 5 from the db rather than remove them in code.

preguntado el 08 de noviembre de 11 a las 19:11

2 Respuestas

You can do it manually. Ignore the property Comments desde Post mapping, and load it manually with Take(X):

// query posts
foreach (var post in posts)
{
    post.Comments = Session.QueryOver<Comments>()
                        .Where(x => x.PostId == post.Id)
                        .Take(X)                       
                        .Future();
}

Con Future en lugar de List it will do one DB roundtrip for all comments.

Respondido 18 Abr '19, 08:04

Should be possible with filters. Read here: http://nhibernate.info/doc/nh/en/index.html#filters

respondido 16 mar '15, 23:03

How are Filters helping here? I do need a TOP/TAKE and not a WHERE clause. - Antineutrino

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.