C#和LINQ:订购一个嵌套查询的值的查询 [英] C# and LINQ: ordering a query by a nested query's value
问题描述
我正在写在ASP.NET即在C#实体框架数据库上面的一个简单的论坛。
每个主题
对象有一个导航属性帖子
指向发表
对象的集合。每个发表
对象有一个属性在
,表示在发表的时候。
Post.Parent
的类型主题
。 Post.Identifier
和 Topic.Identifier
的类型的Int32
。只有发表
有一个在
财产; 主题
有没有这样的属性。 Topic.Parent
是第三种类型论坛
这是由它的引用的标识符$ C 。$ C>
我的问题是这样的:我似乎无法找到一个方法来通过的最后的岗位上做出了所有的主题对象进行排序在每个主题。我试过这样:
<预类=郎-CS prettyprint-覆盖>从T
VAR主题=在context.Topics
从a.Posts
p其中t.Parent.Identifier == forum.Identifier
排序依据p.When降
选择吨;
但我得到复制主题
对象和排序是的不的按最新帖子日期倒序排列。
然后我尝试这样做:
<预类=郎-CS prettyprint-覆盖>
VAR主题=(从吨context.Topics
让lastPost =
(上接第在context.Posts
,其中p.Parent.Identifier == a.Identifier
排序依据p 。当降
选择p).FirstOrDefault()在
,其中t.Parent.Identifier == forum.Identifier
排序依据lastPost降
选择T).Distinct()。
这消除了重复的问题,但仍然没有排序。我甚至尝试从另一个问题一个建议:
<预类=郎-CS prettyprint-覆盖>
VAR主题=(从吨context.Topics
让帖= context.Posts.Where(p =指p .Parent.Identifier == t.Identifier)
让lastPost = posts.OrderByDescending(p => p.When).FirstOrDefault()
,其中t.Parent.Identifier == forum.Identifier
排序依据lastPost.When降
选择T);
不知道下一步该怎么去尝试;似乎这些更高级的LINQ表达式离我而去。 ?任何想法
这应该工作:
VAR主题=从context.Topics
t其中t.Parent.Identifier == forum.Identifier
让lastPost = t.Posts.OrderByDescending(p => p.When )。首先()
排序依据lastPost.When降
选择吨;
(假设主题
一贯一个或更多发表
)
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 this: 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
)
这篇关于C#和LINQ:订购一个嵌套查询的值的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!