简体   繁体   中英

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.

I have to tables:

  • Assignments
  • ForumPosts

These are not very similar, but they both have a "LastUpdated" field. 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).

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.

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).

    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? 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. 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. 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. 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. 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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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