从数据表中拆分层次结构

时间:2018-05-02 05:25:19

标签: c# datatable hierarchy

我有一个数据表,其中包含多个不同高度的层次结构,我需要拆分。

例如。

|---------------------|------------------|
|      Account        | Hierarchy Account|
|---------------------|------------------|
|          1          |                  |
|---------------------|------------------|
|          2          |         1        |
|---------------------|------------------|
|          3          |         1        |
|---------------------|------------------|
|          4          |         2        |
|---------------------|------------------|
|          5          |         3        |
|---------------------|------------------|
|          6          |                  |
|---------------------|------------------
|          7          |         6        |
|---------------------|------------------|
|          8          |         6        |
|---------------------|------------------|

以下是我到目前为止所尝试的内容。

private List<DataTable> SplitDataTablesOnHierarchy(DataTable dataTable)
        {
            List<DataTable> dataTablesList = new List<DataTable>();
            List<string> listTemp = new List<string>();

            var HierarchyAccounts = dataTable.AsEnumerable().Where(m => m.Field<string>("Hierarchy Account Number") == "");

            foreach(var topAccount in TopAccounts )
            {
                //Check if account exists in Hierarchy Account Number
                var topAccountExists = dataTable.AsEnumerable().Any(m => m.Field<string>("Hierarchy Account Number") == topAccount.Field<string>("Account Number"));
                if (topAccountExists == true)
                {
                    DataTable newDataTable = dataTable.Clone();
                    newDataTable.ImportRow(payerAccount);
                    dataTablesList.Add(newDataTable);
                }
                //Top Accounts found and added to tempList
            }
            //CreateDataTable with Top Accounts
            foreach(DataTable dTable in dataTablesList)
            {
                bool bottomHierarchyReached = true;
                var TempSearch = dTable.Rows;

                while(bottomHierarchyReached)
                {
                    foreach(DataRow account in TempSearch)
                    {
                        var rows = dataTable.AsEnumerable().Where(m => m.Field<string>("Hierarchy Account Number") == account.Field<string>("Account Number")).CopyToDataTable();
                        if(rows.Rows.Count == 0)
                        {
                            bottomHierarchyReached = false;
                            break;
                        }
                        else
                        {
                            TempSearch = rows.Rows;
                            dTable.Rows.Add(rows.Rows);
                        }
                    }
                }
            }

            return dataTablesList;
        }

我上面的思考过程是首先找到层次结构中的最高帐户,使用这些帐户创建新的数据表,然后向下钻取并递归添加以下级别到相关的数据表,因为我不知道每个层次结构的高度。 / p>

1 个答案:

答案 0 :(得分:0)

通过创建一个tempList找到一个解决方案,在搜索上面的级别时保留所有较低级别。 一旦完成了通过SearchList的循环,我们就将tempList分配给它。 然后搜索层次结构的下一级。

       foreach (DataTable dTable in dataTablesList)
        {
            bool bottomHierarchyReached = true;
            var SearchList = dTable.AsEnumerable().Select(p=> new { HierarchyAccount = p.Field<string>("Hierarchy Account Number"),
                Account = p.Field<string>("Account Number")
            }).ToList();

            var tempList = SearchList.ToList();
            tempList.Clear();

            while (bottomHierarchyReached)
            {
                tempList.Clear();
                foreach (var account in SearchList)
                {
                    var rows = dataTable.AsEnumerable().Where(m => m.Field<string>("Hierarchy Account Number") == account.Account);
                    if(rows.Count() == 0)
                    {
                        bottomHierarchyReached = false;
                        break;
                    }
                    else
                    {
                        tempList.AddRange(rows.AsEnumerable().Select(p => new {
                            HierarchyAccount = p.Field<string>("Hierarchy Account Number"),
                            Account = p.Field<string>("Account Number")
                        }).ToList());

                        foreach(var row in rows)
                        {
                            dTable.ImportRow(row);
                        }
                    }
                }
                SearchList = tempList.ToList();
            }
        }