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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:22:08+00:00 2026-06-06T06:22:08+00:00

Got datatable with Id, parentId, description. It is a relationial table structure. I want

  • 0

Got datatable with Id, parentId, description. It is a relationial table structure.

I want to be able to pass a parameter to a function which is the current selected Id of the item in in treeview. I want to have a datatable returned with all the related children rows, the top of the relationship is parentId is null… etc etc

I would like to do this LINQ

Any help welcomed.

enter code here var kids = ( from p in dt.AsEnumerable()
                     where p.Field<Int32?>( "ParentId" ) == parentId
                     select new
                     {
                         parent = p,
                         child = from c in dt.AsEnumerable()
                                 where c.Field<Int32?>( "ParentId" ) == p.Field<Int32>( "Id" )
                                 select new
                                 {
                                     child = c
                                 }
                     } ).ToList();

Below is the data I am using and I cannot get it to work as expected. Maybe we are not talking about the same end result or I am missing something terrible.

Here is the code I have and when I pass a value of 57 for the parentId I get 2 rows back in children.

QuotationItemId=58 and 71

I would expect also to get the QuotationItemId 59, 60, 55 ,56, 61

        var lookup = dt.AsEnumerable().ToLookup( p => p.Field<int?>( "ParentId" ) );
        var children = lookup[parentId].ToList();

Data dump

  • 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-06T06:22:09+00:00Added an answer on June 6, 2026 at 6:22 am

    This is what you can do:

    var lookup =
        dt
            .AsEnumerable()
            .ToLookup(p => p.Field<int?>("ParentId"));
    

    Now if you want the root elements do this:

    var roots = lookup[null];
    

    And if you want any children, given the parentId, you do this:

    var children = lookup[parentId];
    

    Simple, huh?


    Here’s some code based on your edit.

    I defined my list of items using an anonymous type:

    var items = new []
    {
        new { QuotationItemId = 54, ParentId = (int?)null, Description = "0000", },
        new { QuotationItemId = 55, ParentId = (int?)60, Description = "Product 55", },
        new { QuotationItemId = 56, ParentId = (int?)60, Description = "Product 56", },
        new { QuotationItemId = 57, ParentId = (int?)54, Description = "Category 57", },
        new { QuotationItemId = 58, ParentId = (int?)57, Description = "Sub Category 58", },
        new { QuotationItemId = 59, ParentId = (int?)58, Description = "Product 59", },
        new { QuotationItemId = 60, ParentId = (int?)58, Description = "Standard Ratel", },
        new { QuotationItemId = 61, ParentId = (int?)60, Description = "Product 61", },
        new { QuotationItemId = 62, ParentId = (int?)null, Description = "Stage 62", },
        new { QuotationItemId = 63, ParentId = (int?)62, Description = "Product 63", },
        new { QuotationItemId = 64, ParentId = (int?)62, Description = "Product 64", },
        new { QuotationItemId = 65, ParentId = (int?)62, Description = "Category 65", },
        new { QuotationItemId = 66, ParentId = (int?)65, Description = "Sub Category66", },
        new { QuotationItemId = 67, ParentId = (int?)66, Description = "Product 67", },
        new { QuotationItemId = 68, ParentId = (int?)66, Description = "Standard Rate 2", },
        new { QuotationItemId = 69, ParentId = (int?)68, Description = "Product 69", },
        new { QuotationItemId = 71, ParentId = (int?)57, Description = "Sub Category 71", },
        new { QuotationItemId = 72, ParentId = (int?)54, Description = "Category 72", },
        new { QuotationItemId = 73, ParentId = (int?)72, Description = "Sub Category73", },
        new { QuotationItemId = 74, ParentId = (int?)73, Description = "Product 74", },
        new { QuotationItemId = 75, ParentId = (int?)73, Description = "Product 75", },
        new { QuotationItemId = 77, ParentId = (int?)null, Description = "qqqqqqqqqq", },
        new { QuotationItemId = 78, ParentId = (int?)null, Description = "zzzzzz", },
        new { QuotationItemId = 79, ParentId = (int?)null, Description = "Test 12345", },
        new { QuotationItemId = 80, ParentId = (int?)null, Description = "456", },
        new { QuotationItemId = 81, ParentId = (int?)null, Description = "tttt", },
        new { QuotationItemId = 82, ParentId = (int?)null, Description = "reddddy777", },
        new { QuotationItemId = 83, ParentId = (int?)null, Description = "bbbbbbbbbbbb", },
        new { QuotationItemId = 84, ParentId = (int?)null, Description = "nnnnnnnnnnnnn", },
    };
    

    And, using LINQPad, the lookup works like so:

    var lookup = items.ToLookup(x => x.ParentId);
    
    lookup[58].Dump();
    lookup[60].Dump();
    

    Screen Dump from LINQPad

    You should note that it doesn’t recurse all the way down.

    If you want to recurse all the way, then you need to define a recursive function. Try this:

    Func<IEnumerable<Quotation>, IEnumerable<Quotation>> recurse = null;
    recurse = qs =>
    {
        return
            qs
                .Concat(
                    from q in qs
                    from q2 in recurse(lookup[q.QuotationItemId])
                    select q2);
    };
    
    recurse(lookup[57]).Dump();
    

    And that gives you:

    LINQPad Image Dump

    Which is what I think you’re expecting.

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

Sidebar

Related Questions

I've got some code which accepts a DataTable as a parameter and calculates the
I've got a DataTable which has a number of records. I've tried the following
I've got an error in a datatable , that I want to present it
I've got a table in SQL 2008 that looks like this: DECLARE @DataTable TABLE
I've got a GridView which reads in data from a DataTable. Every couple of
I have got a datatable which contains many columns in which three are main:
I've got this table: <p:dataTable value=#{requestBean.requestsList} var=requestClass style=width:50px; id=requestList> <p:column> <f:facet name=header> <h:outputText value=ID
as allways i need your help :) i got a DataTable which has the
First of all, I've got a DataTable which stores data in a number of
I got a jquery modal dialog which i load a jquery datatable inside. I

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.