【问题标题】:LINQ-to-NHibernate: Cannot use Linq Skip() and Take() with FetchManyLINQ-to-NHibernate:不能将 Linq Skip() 和 Take() 与 FetchMany 一起使用
【发布时间】:2012-03-28 22:38:29
【问题描述】:

我有这些实体:

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

我想通过Keywords 和 cmets-count 加载BlogPosts 的分页列表。所以我试试这个:

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

您有什么想法可以通过包含Keywords 来获取多个行吗?感谢您的任何建议。


我正在使用NHibernate 3.2 mapping by code

【问题讨论】:

    标签: nhibernate paging linq-to-nhibernate nhibernate-3 nhibernate-projections


    【解决方案1】:

    问题在于 nhibernate linq 提供程序尚未完全实现。

    您可以将跳过/接听电话移动到 ToList() 之后,但随后您将过滤整个结果集,而不是专门查询与该范围匹配的记录。

    或者,您可以使用 QueryOver api,它对 Take 和 Skip 有适当的支持,根据这个答案:https://stackoverflow.com/a/5073510/493

    【讨论】:

    • 谢谢。那么如何通过 QueryOver 加载多对多关联呢? Fetch 方法只支持一对多和一对一的关联!你能帮忙加载post.Keywordspost.Comments.Count吗?
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多