0

I have three DataSets. DataSet 1 has all the parent Ids, DataSet 2 has children of DataSet 1 and DataSet 3 has children of DataSet3.

I want to build a DataSet of tree like structures with Id of DataSet1 as root node.

DataSet 1-

<NewDataSet>
    <Table>
        <Id>A</Id>
        <Desc>ABC</Desc>
    </Table>
    <Table>
        <Id>B</Id>
        <Desc>DEF</Desc>
    </Table>
    <Table>
        <Id>C</Id>
        <Desc>PQR</Desc>
    </Table>
</NewDataSet>

DataSet 2-

<NewDataSet>
    <Table>
        <ParentId>A</ParentId>
        <Id>AA</Id>
        <Desc>ABC</Desc>
    </Table>
    <Table>
        <ParentId>B</ParentId>
        <Id>BB</Id>
        <Desc>DEF</Desc>
    </Table>
    <Table>
        <ParentId>B</ParentId>
        <Id>CB</Id>
        <Desc>PQR</Desc>
    </Table>
</NewDataSet>

DataSet 3-

<NewDataSet>
    <Table>
        <ParentId>AA</ParentId>
        <Id>AAA</Id>
        <Desc>ABC</Desc>
    </Table>
    <Table>
        <ParentId>BB</ParentId>
        <Id>BBB</Id>
        <Desc>DEF</Desc>
    </Table>
    <Table>
        <ParentId>BB</ParentId>
        <Id>CBB</Id>
        <Desc>PQR</Desc>
    </Table>
</NewDataSet>

Result Should be like this if don't specify any root node or if I want the children nodes or not:

GetTree(null, false);

<NewDataSet>
    <Table>
        <Id>A</Id>
        <Desc>ABC</Desc>
        <HasChildren>True</HasChildren>
        <NewDataSet>
            <Table>
                <Id>AA</Id>
                <Desc>ABC</Desc>
                <HasChildren>True</HasChildren>
                <DataSet>
                    <Table>
                        <Id>AAA</Id>
                        <Desc>ABC</Desc>
                        <HasChildren>False</HasChildren>
                    </Table>    
                </DataSet>
            </Table>
        </NewDataSet>
    </Table>
    <Table>
    ..... //All Root Nodes and their children
    </Table>
</NewDataSet>

But If I Provide an Id and also provide if I want the Id's children. GetTree("BB", true);

<NewDataSet>
    <table>
        <Id>BB</Id>
        <Desc>ABC</Desc>
        <HasChildren>True</HasChildren>
        <DataSet>
            <Table>
                <Id>BBB</Id>
                <Desc>ABC</Desc>
                <HasChildren>False</HasChildren>
            </Table>
            <Table>
                <Id>CBB</Id>
                <Desc>PQR</Desc>
                <HasChildren>False</HasChildren>
            </Table>
        </DataSet>
    </table>
</NewDataSet>

GetTree("BB", false);

<NewDataSet>
    <table>
        <Id>BB</Id>
        <Desc>ABC</Desc>
        <HasChildren>True</HasChildren>
    </table>
</NewDataSet>

I know I can do this with Nested Foreach but I want to write a recursive function to perform this operation. I have been searching the Internet for 2 days now for some examples and not able to do this. Any reference to a website would also help me alot.

1 Answer 1

1

Foreach is probably still the way to go, but instead of nested, you would just use it once for each 'node'.

public class MyDataSet
{
    public string ID {get;set;}
    public string Description {get;set;}
    public readonly List<MyDataSet> Children = new List<MyDataSet>();
    public bool HasChildren 
    {
        get { return Children.Count > 0 }
    }
    public void GetTree(string id, bool includeChildren)
    {
        MyDataSet mySet = id == null ? this : Children.FirstOrDefault(child => child.ID == id);

        if(mySet == null) return;
        // TODO: handle mySet

        if(includeChildren)
        {
            foreach (MyDataSet child in Children)
            {
                child.GetTree(null, true);
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.