我正在用ASP编写简单的论坛。净MVC。
在分类视图中,我想显示最新的线程。
按线程排序的代码:
model.ForumThreads = db.ForumThreads
.Where(t => t.ForumThreadCategoryId == id)
.OrderByDescending(t => t.AddDate)
.ToPagedList(page, 10);
ForumPost模型有ForumThread模型的外键
问题是:如何按最后一个帖子排序,但如果没有帖子,那么按帖子添加日期排序。
使用三元if
运算符(如果?
则:
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);