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

  • Home
  • SEARCH
  • 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 1045469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:59:21+00:00 2026-05-16T15:59:21+00:00

I have a data table that contains a one-to-many self referential relationship: Plant {

  • 0

I have a data table that contains a one-to-many self referential relationship:

Plant
{ 
  ID,
  Name,
  ParentID
}

I’m trying to create a linq query that will tell me the total number of descendants stemming from one Plant.

Example:

I have 5 plants:

One {ID = 1, Name = Pine, Parent = null};// This is the root
    Two {ID = 2, Name = Evergreen, Parent = 1}
        Three {ID = 3, Name = Winter Evergreen, Parent = 2}
    Four {ID = 4, Name = Christmas Tree, Parent = 1}
Five {ID = 5, Name = Maple, Parent = null};// This is the root

When I call my LINQ query with an input of ID = 1, I want it to return 3, because there are 3 descendants of One; Two, Three and Four. Five is not a decedent of One.

The only way I can think about doing this involves nested recursive linq queries on the inner results. I have no idea how to do this and I feel as though there is an easier way.

I’m using C# 4.0 and LINQ if that matters.

  • 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-16T15:59:22+00:00Added an answer on May 16, 2026 at 3:59 pm

    If you were using SQL Server, the query you would want constructed would be:

    DECLARE @TargetElementId int
    SET @TargetElementId = 1;
    
    WITH Plants AS
    (
        SELECT ID, Name, ParentId
        FROM PlantsTable
        WHERE ParentId = @TargetElementId
    
        UNION ALL
    
        SELECT ID, Name, ParentId
        FROM PlantsTable
        INNER JOIN Plants ON PlantsTable.ParentId = Plants.ID
    )
    SELECT COUNT(ID) - 1 FROM Plants
    

    I don’t believe this can be done with LinqToSql, see Common Table Expression (CTE) in linq-to-sql?, which is a question of a similar nature.

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

Sidebar

Related Questions

I have a table that contains procedure codes among other data (let's call it
I am building a core data iphone app, and having trouble with retrieving one-many
I am currently working on a MySQL query that contains a table: TBL:lesson_fee -fee_type_id
I hope I can explain this clearly. I have a table, lets call it
I have an ADO .Net Entity Data Model for my database. In this Model
I have the unfortunate task of having to import data from excel into a
I have been tasked with designing an ecommerce solution. The aspect that is causing
how can i know what tableview cell was selected?(being in the detail view) The
Pardon the elementary question but my newness to the realm of database design leaves
I posted this at ServerFault and was told this would be a better spot

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.