ASP.NET MVC - 根据最后一个 post 对论坛主题进行排序
ASP.NET MVC - Sorting forum threads depending on last post
我正在 ASP.NET MVC 中编写简单的论坛。
在类别视图中,我想显示最新的线程。
我的代码按线程添加日期排序:
model.ForumThreads = db.ForumThreads
.Where(t => t.ForumThreadCategoryId == id)
.OrderByDescending(t => t.AddDate)
.ToPagedList(page, 10);
ForumPost 模型具有 ForumThread 模型的外键。
问题是:
如何按最后 post 排序线程,但如果没有 post 则按线程添加日期排序。
使用三元if
运算符(if?
then:
else):
model.ForumThreads = db.ForumThreads
.Where(t => t.ForumThreadCategoryId == id)
.OrderByDescending(t => t.ForumPosts.Any() //if
? t.ForumPosts.Max(x=>x.AddDate) //then by post add date
: t.AddDate) //else like you already do
.ToPagedList(page, 10);
我正在 ASP.NET MVC 中编写简单的论坛。
在类别视图中,我想显示最新的线程。
我的代码按线程添加日期排序:
model.ForumThreads = db.ForumThreads
.Where(t => t.ForumThreadCategoryId == id)
.OrderByDescending(t => t.AddDate)
.ToPagedList(page, 10);
ForumPost 模型具有 ForumThread 模型的外键。
问题是: 如何按最后 post 排序线程,但如果没有 post 则按线程添加日期排序。
使用三元if
运算符(if?
then:
else):
model.ForumThreads = db.ForumThreads
.Where(t => t.ForumThreadCategoryId == id)
.OrderByDescending(t => t.ForumPosts.Any() //if
? t.ForumPosts.Max(x=>x.AddDate) //then by post add date
: t.AddDate) //else like you already do
.ToPagedList(page, 10);