2

これらの単純なクラスに問題があります。

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 クエリが必要です。と を持つ Entity Framework DbContext を想定するThreadsPosts、どのように記述しますか? グループ化は簡単です:

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
4

2 に答える 2

4

あなたが使用することができます:

from t in Threads
group t.Posts by t into tg
orderby tg.Max(post => post.Posted) // Order by the latest post per thread
select tg.Key;

明らかにdescending、ほとんど最近投稿されたスレッドで最初にスレッドを注文したい場合に使用します。

于 2012-04-12T11:36:08.157 に答える
0

また、試すことができます:

var orderedThread1 = from t in threads
                     from p in t.Posts
                     orderby p.Posted ascending 
                     select t;

var orderedThread2 = from t in threads
                     group t.Posts by t
                     into tp
                     orderby tp.Max(posts => posts.Max(p => p.Posted))
                     select tp.Key;
于 2012-04-12T11:55:18.820 に答える