我正在尝试订购新闻实体的集合,并且每个项目都有我想要订购的 NewsComment 的子集合。我尝试了一些方法,这就是我现在所拥有的,我做错了什么?
我尝试使用 foreach linq 对列表进行排序,但没有成功。
public ActionResult Index()
{
var news = db.News.ToList();
// Order NewsComments?, date stored as datetime2 for NewComment object
news.ForEach(x => x.NewsComments.OrderByDescending(z => z.Date).ThenBy(d => DateTime.Now));
HomeView homeView = new HomeView()
{
News = news.OrderByDescending(x => DateTime.Parse(x.Date)).ThenBy(x => DateTime.Now.ToString()).ToList(), // Pulling news and ordering by date(stored as string)
GameServerNodes = db.GameServerNodes.Where(x => x.Visible == 1).ToList(),
GameServerTypes = db.GameServerTypes.OrderBy(x => x.Name).ToList(),
GameServersCount = db.GameServers.Count()
};
ViewBag.IsAdmin = steamAuthentication.IsAdmin(HttpContext);
return View(homeView);
}
这似乎根本没有改变新闻评论的顺序。新闻项目订购正常。
我的 lamda 表达式中没有作业。这是我最终使用的
news.ForEach(x => x.NewsComments = x.NewsComments.OrderBy(z => z.Date).ThenBy(d => DateTime.Now).ToList());