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 9319739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:27:34+00:00 2026-06-19T03:27:34+00:00

Been looking for a solution for this but haven’t been able to find one

  • 0

Been looking for a solution for this but haven’t been able to find one so far.

I’m fairly sure its possible with one linq call but having trouble working it out.

I have the following data structure

Id      ParentId     Name       ValidFlag

1       NULL         parent 1   1
2       NULL         parent 2   1
3       NULL         parent 3   0
4       1            child 1    1
5       1            child 2    1
6       2            child 3    1
7       2            child 4    1
8       3            child 5    1
9       3            child 6    1

Now what i want to do is return all valid parents and their children so this case i would return everything except Id = 3 (parent 3).

Is there a simple way to do this with LINQ? Im guessing there is but my LinqIQ is very limited, i know the basics but beyond that i need much help.

Is this a case for ToLookup() or Union or neither?

Update:

To be more specific as I’ve failed to do so, both types of records are in the same table, i want to return all records whether its a parent or child in the 1 query if possible.

Its not as simple as just selecting all records where ValidFlag = 1. The source database is a mess and the only way to get all records is to find the “valid” parents then find all children for the “valid” parents. I know i can just do a simple query to return all valid parent records then do a separate query to find all child of those parents, but combining into 1 LINQ query is where i fail, i was hoping that is possible. In this case it is possible that there are valid child entries of invalid parents, hence need for the question

  • 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-06-19T03:27:35+00:00Added an answer on June 19, 2026 at 3:27 am

    This should do the trick, (edit: see below for version that doesn’t use Distinct.)

    (from parents in collection
    from all in collection
    where
        parents.ValidFlag == 1 &&
        parents.ParentId == null &&
        all.ValidFlag == 1 &&
        (all.ParentId == null || all.ParentId == parents.Id)
    select all).Distinct();
    

    The above code should hopefully generate something quite similar to
    what it itself looks like in SQL, maybe with the exception of the
    distinct which might cause it to return more data that is actually
    needed to be filtered on the client. Something that might become an
    issue if there’s a lot data, chiefly if there’s a lot of parents
    because it will return duplicates of those)

    Here is the query reworked without the distinct call

    from parents in collection // parents is only used to decide which children to get
    from all in collection // this is where we will actually grab our data from
    where
        parents.ValidFlag == 1 &&  // only include parents that are valid
        parents.ParentId == null && // and that are parents
        all.ValidFlag == 1 &&  // only include entries that are valid
        (
            (all.ParentId == null && all.Id == parents.Id) ||  // If entry is a parent, match with itself to limit returns to 1
            all.ParentId == parents.Id // otherwise, parentid should match one of the valid parents.
        )
    select all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly sure this has a simple solution, but I haven't been able to
I've been looking around for a solution to this, but haven't quite found one.
I've been looking around for a solution to this thing but I haven't found
I've been looking for some hours but haven't found a solution about this issue.
I've been looking for a solution to this but haven't found anything that will
I have been looking all over stackoverflow to find a solution, but I haven't
I've been looking for a while but i haven't been able to do this.
I'm sorry, but I haven't been able to find a working solution from any
I have been looking for a solution, and haven't been lucky to find one.
So I've been looking for a good solution everywhere but did not find anything

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.