1

我有一张表格RecentChanges,其中包含在我的网站上完成的所有操作的参考。它包含已完成操作的类型(例如编辑、上传等)、修订 ID 外键(引用Revisions表中的 ID - 可以为空)和日志 ID 外键(引用表中的 ID)Logs表 - 可以为空)。提到的最后两个表都包含操作发生时间的时间戳。

我的问题是,由于时间戳值位于两个单独的表中,我如何对RecentChanges表中的结果进行排序以按降序显示?

为了得到一个想法,这里有一段 Linq 查询:

var list = (from r in Entity.RecentChanges
            //where clause??
            select r);
4

1 回答 1

1

像这样的东西?我正在投影到一个匿名类型以帮助排序(您需要在投影中添加您需要的其他字段):

var query = (from c in Entity.RecentChanges
        from l in Entity.Logs
            .FirstOrDefault(log => log.ID == c.LogID).DefaultIfEmpty()
        from r in Entity.Revisions
            .FirstOrDefault(rev => rev.ID == c.RevisionID).DefaultIfEmpty()
        select new
               {
                   ActivityDate = l != null 
                       ? l.Timestamp 
                       : r != null
                           ? r.Timestamp
                           : DateTime.Now. //what if both are null? catching all eventualities here
                   //select your other fields here

               })
        .OrderByDescending(c => c.ActivityDate)
        .ToList();
于 2012-04-30T19:36:58.193 回答