我正在写简单的论坛在ASP.NET的MVC。
在类别视图中,我想要显示最新的线程。
我的按线程排序的代码添加日期:
model.ForumThreads = db.ForumThreads
.Where(t => t.ForumThreadCategoryId == id)
.OrderByDescending(t => t.AddDate)
.ToPagedList(page, 10);ForumPost模型具有ForumThread模型的外键。
问题是:如何按最后一篇文章对线程进行排序,但如果没有帖子,则按线程添加日期排序。
发布于 2016-08-19 18:28:14
使用三元if运算符(如果为?,则为:):
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);https://stackoverflow.com/questions/39036356
复制相似问题