C# and LINQ: ordering a query by a nested query's value
I'm writing a simple forum in ASP.NET that sits on top of an Entity Framework database in C#.
Each Topic
object has a navigation property Posts
pointing to a collection of Post
objects. Each Post
object has a property When
that indicates when the post was made.
Post.Parent
is type Topic
. Post.Identifier
and Topic.Identifier
is type Int32
. Only Post
has a When
property; Topic
has no such property. Topic.Parent
is a third type Forum
that is referenced by its Identifier
.
My problem is th开发者_运维百科is: I can't seem to find a way to sort all Topic objects by the last post made in each topic. I've tried this:
var topics = from t in context.Topics
from p in a.Posts
where t.Parent.Identifier == forum.Identifier
orderby p.When descending
select t;
But I got duplicate Topic
objects and sorting was not by latest post date descending.
I then tried this:
var topics = (from t in context.Topics
let lastPost =
(from p in context.Posts
where p.Parent.Identifier == a.Identifier
orderby p.When descending
select p).FirstOrDefault().When
where t.Parent.Identifier == forum.Identifier
orderby lastPost descending
select t).Distinct();
It eliminated the duplicate problem, but still no sorting. I even tried a suggestion from another question:
var topics = (from t in context.Topics
let posts = context.Posts.Where(p => p.Parent.Identifier == t.Identifier)
let lastPost = posts.OrderByDescending(p => p.When).FirstOrDefault()
where t.Parent.Identifier == forum.Identifier
orderby lastPost.When descending
select t);
Not sure what to try next; it seems that these more advanced LINQ expressions escape me. Any ideas?
This should work:
var topics = from t in context.Topics
where t.Parent.Identifier == forum.Identifier
let lastPost = t.Posts.OrderByDescending(p => p.When).First()
orderby lastPost.When descending
select t;
(assuming a Topic
has always one or more Post
)
精彩评论