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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:18:22+00:00 2026-05-11T01:18:22+00:00

I have a self-referencing table with an Id, CategoryName, and ParentId. It’s a typical

  • 0

I have a self-referencing table with an Id, CategoryName, and ParentId. It’s a typical scenario of a hierarchy table of categories that can themselves be divided into categories that DB experts tell me is called the adjacency model.

What I want is to use Linq to SQL to query for subcategories that themselves are related to no other subcategories, ie they are immediate leaf nodes of some given category or subcategory.

The easy part, I got, which is just getting the subcategories. Almost embarrassed to put the code here. But we do like to see code..

IList<Categories> subcategories = context.Where( c => c.ParentId == 1).ToList();   

But narrowing it to categories with no subcategories is turning me around. Any help would be much appreciated.

Thanks for you help. Jeff

UPDATE** It would appear this works, but if someone could confirm that it is ‘proper’ I’d be grateful. So, if I want leaf nodes under a category with Id = 1, I would do this:

Categories.Where( c => !c.Children.Any ( d => d.ParentId == c.Id)).Where( e => e.ParentId == 1)  

‘Children’ is the name Linq gives the self-referencing association.

  • 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. 2026-05-11T01:18:22+00:00Added an answer on May 11, 2026 at 1:18 am

    Your solution is correct because the Any() method, translates into sql ‘EXISTS()’ function and !c.Children.Any ( d => d.ParentId == c.Id)) translates into a sql clause similar to NOT EXISTS (SELECT * FROM Categories WHERE ParentID = outerRef.ID)

    Another way to do it is to use Count :

    Categories.Where( c => c.Children.Count(d => d.ParentId == c.Id) == 0).Where( e => e.ParentId == 1) 

    But usually EXISTS() is preferred to COUNT() in sql (for performance reasons) so the solution with Any() should be the right one.

    • 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 named categories that has a parentcategoryid column that
I have a typical self-referencing table of employees. How can you build an UDF
I have a self-referencing table (Customers) and a table that will link to one
I have a self referencing table in Oracle 9i, and a view that gets
I have a standard self referencing table of Categories . In my entity model
i have a self referencing table called project that has a master detail relationship
I have a self referencing table and I want a mysql query that will
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I have a simple self referencing table as depicted here: CREATE TABLE [dbo].[Project]( [ProjectId]
I currently have a self-referencing join table without a primary key. If you're familiar

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.