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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:32:25+00:00 2026-05-13T23:32:25+00:00

LINQ gurus, I am looking for help to write a query… I have a

  • 0

LINQ gurus, I am looking for help to write a query…

I have a table with Person records, and it has a nullable ParentID column, so it is kind of self-referencing, where each record might have a Parent.

I am looking for unprocessed rows whose parent rows were processed.
This SQL works fine:

SELECT *
  FROM Person
  where IsProcessed = 0 and
  ParentId in
  (
  select Id from Person 
  where IsProcessed = 1
  )

I tried a number of LINQ queries, but they failed. Now, I’m trying:

    var qParent = 
                from parent in db.Person
                where 
                parent.IsProcessed == true
                select parent.ID;

    var qChildren = from child in db.Person
                    where
                    child.IsProcessed == false
                    && child.ParentId.HasValue
                    select child.ParentId.Value;

    var q2 = qChildren.Intersect(qParent);

This yields SQL with a DISTINCT clause, for some reason, and I am baffled why DISTINCT is generated.

My main question is how to write LINQ for the SQL statement above?

Thanks in advance.

  • 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-13T23:32:25+00:00Added an answer on May 13, 2026 at 11:32 pm

    Intersect is a set operation – it is meant to return a set of distinct elements from the intersection. It seems reasonable to me that it would use DISTINCT in the SQL. There could be multiple children with the same parent, for example – Intersect should only return that ID once.

    Is there any reason you don’t want to use a join here?

    var query = from parent in db.Person
                where parent.IsProcessed
                join child in db.Person.Where(child => !child.IsProcessed)
                   on parent.ID equals child.ParentId.Value
                select child;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq query that will search a column for a word and
LINQ to SQL allows table mappings to automatically convert back and forth to Enums
LINQ simplifies database programming no doubt, but does it have a downside? Inline SQL
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
Can LINQ to SQL query using NOT IN ? e.g., SELECT au_lname, state FROM
Using LINQ to Entities sounds like a great way to query against a database
This should be and easy one for the LINQ gurus out there. I'm doing
In LINQ to SQL, is it possible to check to see if an entity
In Linq to SQL it is possible to generate the database from the dbml

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.