无论如何要选择最近的帖子和最近的帖子

时间:2014-04-02 20:28:55

标签: c# asp.net-mvc linq entity-framework

我正在使用LINQ并将此命令链接到模型中。

model.LatestPosts = db.TPGForums.SelectMany(m => m.TPGForumTopics)
                                .SelectMany(m => m.TPGForumPosts)
                                .SelectMany(m => m.TPGForumThreads)
                                .OrderByDescending(m => m.dateCreated)
                                .Take(5);

但这只会让我回到主题。我需要TPGForumPostsTPGForumThreads中最近5个最近的项目。我如何返回最近的5个帖子/帖子而不仅仅是最新的帖子?

1 个答案:

答案 0 :(得分:1)

嵌套SelectMany来电,而不是将它们链接起来:

var query = db.TPGForums
    .SelectMany(forum => forum.TPGForumTopics
        .SelectMany(topic => topic.TPGForumPosts
            .SelectMany(post => post.TPGForumThreads
                .Select(thread => new { forum, topic, post, thread }))))
    .OrderByDescending(m => m.thread.dateCreated)
    .Take(5);