Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6371569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:05:29+00:00 2026-05-25T01:05:29+00:00

I have a self referencing table Product with the following structure (where D =

  • 0

I have a self referencing table “Product” with the following structure (where D = Draft and A = Approved)

ID   ParentID  Status  Name
---------------------------
1    NULL      A       Foo
2    1         A       Foo2
3    NULL      D       Bar
4    1         D       Foo3

A row can either be “new” (where ParentID == null) or can be a version of an existing row. So we can see from the table that there are 3 versions for the item “Foo” and only 1 for “Bar”.

I need a way of returning the latest versions of each item based on whether the user is able to see only “Approved” items or is able to see “Draft” as well. So for example

Users who can see “D” would have:

3    NULL    D
4    1       D

The “latest” row for “Foo” and “Bar”.

Users who can see “A” would have:

2    1       A

ie. only the “Approved” versions.

Thanks in advance,

Jose

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:05:30+00:00Added an answer on May 25, 2026 at 1:05 am

    Here is the Linq query that should work for you:

    bool hasDraftAccess = false;
    
    var query = DataContext.Records.AsQueryable();
    
    if (!hasDraftAccess) {
        query = query.Where(r => r.Status == 'A');
    }
    
    var seriesQuery = query.Select(r => new { Record = r, SeriesID = r.ParentID ?? r.ID });
    var latestQuery = seriesQuery.GroupBy(s => s.SeriesID).Select(g => g.OrderByDescending(s => s.Record.ID).First());
    var resultsQuery = latestQuery.Select(s => s.Record);
    var results = resultsQuery.ToArray();
    

    Here’s what’s happening:

    1. First, add a WHERE clause to filter out draft records if the user doesn’t have access to them
    2. Then add a pseudo column called ‘SeriesID’ that groups all the related versions into that one column. That is, make it easy to group parent and related children.
    3. Group the related records and then pick whichever record is most recent
    4. Select the Linq Entity from the anonymous type so that it is updatable

    I should note that if you have the ability to change your data schema you should consider adding a column called InsertDate or something to that effect. Right now I am assuming that whatever record has the highest ID is the latest. It is often better to add a DateTime field and sort on that instead.

    I apologize that this isn’t actually using Linq syntax–I prefer fluent coding styles–but it could be easily translated to Linq syntax if you preferred it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a self-referencing table with an Id, CategoryName, and ParentId. It's a typical
I have a organization name table with the following structure given below: CREATE TABLE
I have a table in my source DB that is self referencing |BusinessID|...|ParentID| This
I have a self-referencing table with content like this: Self-referencing parent table ID ParentID
I have a self referencing table named categories that has a parentcategoryid column that
I have a self referencing table in Oracle 9i, and a view that gets
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I have a self-referencing table 'comments' where comments.replyToId REFERENCES comments.ID. My question is, how
I have a standard self referencing table of Categories . In my entity model
I have a self-referencing table (Customers) and a table that will link to one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.