我正在使用LINQ并将此命令链接到模型中。
model.LatestPosts = db.TPGForums.SelectMany(m => m.TPGForumTopics)
.SelectMany(m => m.TPGForumPosts)
.SelectMany(m => m.TPGForumThreads)
.OrderByDescending(m => m.dateCreated)
.Take(5);
但这只会让我回到主题。我需要TPGForumPosts
和TPGForumThreads
中最近5个最近的项目。我如何返回最近的5个帖子/帖子而不仅仅是最新的帖子?
答案 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);