LINQ-to-NHibernate: Cannot use Linq Skip() and Take() with FetchMany

匆匆过客 提交于 2019-12-14 02:06:54

问题


I have these entities:

public class BlogPost {
    public virtual int Id { get; set; }
    public virtual IList<Keyword> Keywords { get; set; }
    public virtual IList<BlogComment> Comments { get; set; }
}

public class BlogComment {
    public virtual int Id { get; set; }
    public virtual BlogPost Post { get; set; }
}

public class Keyword {
    public virtual int Id { get; set; }
    public virtual IList<BlogPost> BlogPosts { get; set; }
}

I want to load a paged-list of BlogPosts by their Keywords and comments-count. So I try this:

var entities = session.Query<BlogPost>()
    .Where(t => t.Published)
    .FetchMany(t => t.Keywords)
    .OrderByDescending(t => t.UpdatedAt)
    .Skip((pageNumber - 1) * pageSize).Take(pageSize)
    .Select(t => new {
        CommentsCount = t.Comments.Count(),
        Post = t
    })
    .ToList();

But the folowing error occurs:

Specified method is not supported.

And when I remove .Skip((pageNumber - 1) * pageSize).Take(pageSize) it works! e.g.

var entities = session.Query<BlogPost>()
    .Where(t => t.Published)
    .FetchMany(t => t.Keywords)
    .OrderByDescending(t => t.UpdatedAt)
    // remove the below line
    //.Skip((pageNumber - 1) * pageSize).Take(pageSize)
    .Select(t => new {
        CommentsCount = t.Comments.Count(),
        Post = t
    })
    .ToList();

Have you any idea please to take a number of rows by including Keywords? Thanks for any suggestion.


I'm using NHibernate 3.2 mapping by code.


回答1:


The problem is that the nhibernate linq provider isn't fully implemented yet.

You could move the skip / take calls to be after the ToList() but then you're going to be filtering on the entire result set rather than querying specifically for the records matching that range.

Alternatively you could use the QueryOver<> api which has proper support for Take and Skip as per this answer: https://stackoverflow.com/a/5073510/493




回答2:


This should now be supported in 3.3.3.GA

http://sourceforge.net/p/nhibernate/news/2013/03/nhiberate-333ga-released/



来源:https://stackoverflow.com/questions/9668608/linq-to-nhibernate-cannot-use-linq-skip-and-take-with-fetchmany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!