我有这些实体:
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; }
}
我想加载一个按Keyword
s和评论数分页的BlogPost
s列表。所以我试试这个:
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();
但出现以下错误:
不支持指定的方法。
当我删除.Skip((pageNumber - 1) * pageSize).Take(pageSize)
时,它就起作用了!例如
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();
你有什么想法吗?请把Keyword
包括在内,排几行?谢谢你的建议。
我用的是NHibernate 3.2 mapping by code
。
问题是nhibernate linq提供程序尚未完全实现。
您可以将skip/take调用移动到ToList()的之后的,但随后您将对整个结果集进行筛选,而不是专门查询与该范围匹配的记录。
或者,您可以使用QueryOver<>根据这个答案,api对Take和Skip有适当的支持:https://stackoverflow.com/a/5073510/493
这应该在3.3.3.GA 中得到支持
http://sourceforge.net/p/nhibernate/news/2013/03/nhiberate-333ga-released/