我简要地说这些表格:
表:RPG
Coloumns:RPGID,姓名
表:RPGCharacter
Coloumns:CharID,RPGID,姓名
表:RPGPosts
Coloumns:PostID,CharID,Text,timestamp
所以在数据库中关系是: 1个RPG有X个字符,1个字符有X个帖子。
我通过以下代码获取信息:
public static List<RPG> GetAllRPGs()
{
using (RPGDataContext dc = new RPGDataContext())
{
return (from a in dc.RPGs where !a.IsHided && !a.IsDeleted select a).ToList();
}
}
问题是: 如何按最后一篇文章(RPGPOst-Table的TIMESTAMP-Coloumn)对这些列表进行排序?
答案 0 :(得分:1)
假设您已经设置了适当的导航属性,那么这样的事情应该有效:
var results =
(from a in dc.RPGs
where !a.IsHided && !a.IsDeleted
orderby a.Characters.SelectMany(c => c.Posts).Select(p => p.Timestamp).Max()
select a)
.ToList();
或者可能:
var results =
(from a in dc.RPGs
where !a.IsHided && !a.IsDeleted
orderby a.Characters
.SelectMany(c => c.Posts)
.OrderByDescending(p => p.Timestamp)
.Select(p => p.Timestamp)
.FirstOrDefault()
select a)
.ToList();