我对这些简单的类有疑问:
public class Thread
{
public string Title { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public DateTime Posted { get; set; }
public string Text { get; set; }
}
我想要一个 LINQ 查询,它将返回所有线程,按最新发布顺序排序。假设一个实体框架 DbContext 带有Threads
and Posts
,如何编写它?分组很简单:
from t in Threads
group t.Posts by t into tg
select tg.Key;
但是如何根据最新的线程对线程进行排序Post.Posted
?
编辑 - 基于 Jons 答案的解决方案:
from t in Threads
from p in t.Posts
group p by t into tg
orderby tg.Max(p => p.Posted) descending
select tg.Key