简体   繁体   English

最近的记录有2个表和take / skip

[英]Most recent records with 2 tables and take / skip

What I want to do, is basically what this question offers: SQL Server - How to display most recent records based on dates in two tables .. Only difference is: I am using Linq to sql. 我想做的,基本上是这个问题提供的: SQL Server - 如何根据两个表中的日期显示最近的记录 ..唯一的区别是:我使用Linq to sql。

I have to tables: 我要表:

  • Assignments 分配
  • ForumPosts ForumPosts

These are not very similar, but they both have a "LastUpdated" field. 这些不是很相似,但它们都有一个“LastUpdated”字段。 I want to get the most recent joined records. 我想获得最新的联合记录。 However, I also need a take/skip functionality for paging (and no, I don't have SQL 2012). 但是,我还需要一个take / skip功能来进行分页(不,我没有SQL 2012)。

I don't want to create a new list (with ToList and AddRange) with ALL my records, so I know the whole set of records, and then order.. That seems extremely unefficient. 我不想创建一个包含所有记录的新列表(带有ToList和AddRange),所以我知道整个记录集,然后命令..这似乎非常低效。

My attempt: 我的尝试:

Please don't laugh at my inefficient code.. Well ok, a little (both because it's inefficient and... it doesn't do what I want when skip is more than 0). 请不要嘲笑我的低效代码..好吧,一点点(因为它效率低下......当跳过超过0时它不能做我想要的)。

    public List<TempContentPlaceholder> LatestReplies(int take, int skip)
    {
        using (GKDBDataContext db = new GKDBDataContext())
        {
            var forumPosts = db.dbForumPosts.OrderBy(c => c.LastUpdated).Skip(skip).Take(take).ToList();
            var assignMents = db.dbUploadedAssignments.OrderBy(c => c.LastUpdated).Skip(skip).Take(take).ToList();

            List<TempContentPlaceholder> fps =
                forumPosts.Select(
                    c =>
                    new TempContentPlaceholder()
                    {
                        Id = c.PostId,
                        LastUpdated = c.LastUpdated,
                        Type = ContentShowingType.ForumPost
                    }).ToList();

            List<TempContentPlaceholder> asm =
                                assignMents.Select(
                                    c =>
                                    new TempContentPlaceholder()
                                    {
                                        Id = c.UploadAssignmentId,
                                        LastUpdated = c.LastUpdated,
                                        Type = ContentShowingType.ForumPost
                                    }).ToList();

            fps.AddRange(asm);

            return fps.OrderBy(c=>c.LastUpdated).ToList();

        }
    }

Any awesome Linq to SQl people, who can throw me a hint? 任何真棒的Linq到SQl的人,谁能给我一个暗示? I am sure someone can join their way out of this! 我相信有人可以加入他们的方式!

First, you should be using OrderByDescending , since later dates have greater values than earlier dates, in order to get the most recent updates. 首先,您应该使用OrderByDescending ,因为以后的日期具有比早期日期更大的值,以便获得最新的更新。 Second, I think what you are doing will work, for the first page, but you need to only take the top take values from the joined list as well. 第二,我觉得你在做什么工作,第一页,但你只需要取前take值从加入名单。 That is if you want the last 20 entries from both tables combined, take the last 20 entries from each, merge them, then take the last 20 entries from the merged list. 也就是说,如果您希望将两个表中的最后20个条目组合在一起,则从每个表中取出最后20个条目,合并它们,然后从合并列表中获取最后20个条目。 The problem comes in when you attempt to use paging because what you will need to do is know how many elements from each list went into making up the previous pages. 当您尝试使用分页时会出现问题,因为您需要知道每个列表中有多少元素用于组成前一页。 I think, your best bet is probably to merge them first, then use skip/take. 我认为,你最好的选择可能是首先将它们合并,然后使用skip / take。 I know you don't want to hear that, but other solutions are probably more complex. 我知道你不想听到这个,但其他解决方案可能更复杂。 Alternatively, you could take the top skip+take values from each table, then merge, skip the skip values and apply take . 或者,您可以从每个表中获取顶部skip+take值,然后合并,跳过skip值并应用take

using (GKDBDataContext db = new GKDBDataContext())
{
     var fps = db.dbForumPosts.Select(c => new TempContentPlaceholder()
                {
                    Id = c.PostId,
                    LastUpdated = c.LastUpdated,
                    Type = ContentShowingType.ForumPost
                })
                .Concat( db.dbUploadedAssignments.Select(c => new TempContentPlaceholder()
                {
                    Id = c.PostId,
                    LastUpdated = c.LastUpdated,
                    Type = ContentShowingType.ForumPost
                }))
                .OrderByDescending( c => c.LastUpdated )
                .Skip(skip)
                .Take(take)
                .ToList();

    return fps;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM