1
votes

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

2

2 Answers

5
votes

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.

-1
votes

You can select them simply with:

var kids = from p in db.Items
           where p.parentId == parentId
           select p

Then you will have: kids.Items as a list. So you can retrieve children by a simple foreach loop like this:

foreach(Item item in kids.Items)

Hope it helps.