优化TreeView方法

时间:2019-02-18 18:03:26

标签: c#

我有一个非常大的方法来插入TreeView的子级,基本上我会执行两个不同的查询,并且几乎没有更改。 firs foreach插入父级的第一个孩子,第二个foreach插入之前创建的孩子的孩子。

Foreach查询只是更改条件AND [ParentDesignKey] IS NULL,但其中的变量太相似了。

因此,在这里,我只有两个不同的foreach,但是我需要再添加4个第一个foreach(第二个foreach)子项,并且我不希望使用很大的方法。有什么方法可以对此进行优化?

完整方法:

private void AssignDesign(DataTable designItemList, DataTable designFolioList, TreeNode parent, string designName, string legacyKey, int indexProject)
        {
            var iDesign = 0;


            foreach (var currentDesignItem in designItemList.Select($"[Design Name] = '{designName}' AND [Project Number] = {legacyKey}  AND [ParentDesignKey] IS NULL"))
            {
                var designItemName = currentDesignItem["DesignItemName"] as string;
                var designItemKey = currentDesignItem["DesignKey"] as int?;
                var designItemStatus = currentDesignItem["Design Status"] as string;
                var designItemIsLocked = currentDesignItem["IsLocked"] as bool?;
                var designItemStatusKey = currentDesignItem["StatusKey"] as int?;
                var designItemCreated = currentDesignItem["Design Created"] as DateTime?;
                var designItemRunCount = currentDesignItem["RunCount"] as int?;
                var designItemRunUnConCount = currentDesignItem["RunUnconCnt"] as int?;
                var designItemShopsStatus = currentDesignItem["ShopsStatus"] as int?;
                var designItemShopStatusExtended = currentDesignItem["ShopStatusExtended"] as string;
                var designItemFolio = designFolioList.Select($"[DesignKey] ={designItemKey}").FirstOrDefault()["Folio"].ToString();


                if (designItemName is null || designItemShopsStatus is null || designItemShopStatusExtended is null || designItemKey is null || designItemStatus is null || designItemStatusKey is null || designItemCreated is null || designItemRunCount is null || designItemRunUnConCount is null || designItemIsLocked is null) continue;



                var projectIndex = parent.Index;
                parent.Nodes[indexProject].Nodes.Add(designItemName);
                parent.Nodes[indexProject].Nodes[iDesign].Tag = $"{designItemKey.Value}|{projectIndex}|BDCD";
                parent.Nodes[indexProject].Nodes[iDesign].Name = "Design";
                parent.Nodes[indexProject].Nodes[iDesign].ToolTipText = $"Design: {designItemName} - Folio: {designItemFolio}";
                parent.Nodes[indexProject].Nodes[iDesign].ContextMenuStrip = contextMenuDesign;

                //load icon for design item
                this.LoadDesignStatusNodeIcon(parent.Nodes[indexProject].Nodes, iDesign, designItemStatusKey.Value, designItemIsLocked.Value);
                var parentNodes = parent.Nodes[indexProject].Nodes[iDesign].Nodes;
                parentNodes.Add(new TreeNode($"{designItemCreated.Value.ToLongDateString()} ({designItemCreated.Value.ToString("yyMMdd")})", 8, 8)
                {
                    Name = "Design Created",
                    ToolTipText = "Created Date"
                });
                parentNodes.Add(new TreeNode(designItemStatus, 18, 18)
                {
                    Name = "Design Status",
                    ToolTipText = "Design Status",
                    Tag = $"{designItemStatusKey.Value}|{designItemIsLocked}"
                });
                var imgKey = LoadShopsNodeIcon(designItemShopsStatus.Value);

                parentNodes.Add(new TreeNode(designItemShopStatusExtended, imgKey, imgKey)
                {
                    Name = "Shops Drawing Status",
                    ToolTipText = "Shops Drawing Status",
                    Tag = designItemShopsStatus.Value
                });

                if (designItemRunCount.Value == 0 && designItemRunUnConCount.Value > 0)
                {
                    parentNodes.Add(new TreeNode("Unconsolidated Runs: " + designItemRunUnConCount.Value)
                    {
                        Name = "Unconsolidated Runs",
                        ToolTipText = "Unconsolidated Runs",
                        Tag = designItemRunUnConCount.Value
                    });
                }
                else
                {
                    parentNodes.Add(new TreeNode("Run Count: " + designItemRunCount.Value)
                    {
                        Name = "Run Count",
                        ToolTipText = "Run Count",
                        Tag = designItemRunCount.Value
                    });
                }

                var iChangeOrderDesign = 0;
                foreach (var item in designItemList.Select($"[Design Name] = '{designName}' AND [Project Number] = {legacyKey} AND [ParentDesignKey] = {designItemKey}"))
                {
                    var changeOrderDesignItemName = item["DesignItemName"] as string;
                    var changeOrderDesignItemKey = item["DesignKey"] as int?;
                    var changeOrderDesignItemStatus = item["Design Status"] as string;
                    var changeOrderDesignItemIsLocked = item["IsLocked"] as bool?;
                    var changeOrderDesignItemStatusKey = item["StatusKey"] as int?;
                    var changeOrderDesignItemCreated = item["Design Created"] as DateTime?;
                    var changeOrderDesignItemRunCount = item["RunCount"] as int?;
                    var changeOrderDesignItemRunUnConCount = item["RunUnconCnt"] as int?;
                    var changeOrderDesignItemShopsStatus = item["ShopsStatus"] as int?;
                    var changeOrderDesignItemShopStatusExtended = item["ShopStatusExtended"] as string;
                    var changeOrderDesignItemFolio = designFolioList.Select($"[DesignKey] ={changeOrderDesignItemKey}").FirstOrDefault()["Folio"].ToString();
                    var changeOrderParentDesignKey = item["ParentDesignKey"] as int?;

                    if (changeOrderDesignItemName is null || changeOrderDesignItemShopsStatus is null || changeOrderDesignItemShopStatusExtended is null || changeOrderDesignItemKey is null || changeOrderDesignItemStatus is null || changeOrderDesignItemStatusKey is null || changeOrderDesignItemCreated is null || changeOrderDesignItemRunCount is null || changeOrderDesignItemRunUnConCount is null || changeOrderDesignItemIsLocked is null) continue;


                    //Create lowest level and assign their tree nodes

                    projectIndex = parent.Index;

                    TreeNode tn = parent.Nodes[indexProject].Nodes[iDesign].Nodes.Add(changeOrderDesignItemName);
                    parent.Nodes[indexProject].Nodes[iDesign].Nodes[tn.Index].Tag = $"{changeOrderDesignItemKey.Value}|{projectIndex}|CO";
                    parent.Nodes[indexProject].Nodes[iDesign].Nodes[tn.Index].Name = "Design";
                    parent.Nodes[indexProject].Nodes[iDesign].Nodes[tn.Index].ToolTipText = $"Change Order: {changeOrderDesignItemName} - Folio: {changeOrderDesignItemFolio}";
                    parent.Nodes[indexProject].Nodes[iDesign].Nodes[tn.Index].ContextMenuStrip = contextMenuDesign;


                    //load icon for changeOrderDesign item

                    var changeOrderparentNodes = parent.Nodes[indexProject].Nodes[iDesign].LastNode.Nodes;
                    changeOrderparentNodes.Add(new TreeNode($"{changeOrderDesignItemCreated.Value.ToLongDateString()} ({changeOrderDesignItemCreated.Value.ToString("yyMMdd")})", 8, 8)
                    {
                        Name = "Change Order Design Created",
                        ToolTipText = "Created Date"
                    });
                    changeOrderparentNodes.Add(new TreeNode(changeOrderDesignItemStatus, 18, 18)
                    {
                        Name = "Design Status",
                        ToolTipText = "Design Status",
                        Tag = $"{changeOrderDesignItemStatusKey.Value}|{changeOrderDesignItemIsLocked}"
                    });
                    var changeOrderimgKey = LoadShopsNodeIcon(changeOrderDesignItemShopsStatus.Value);

                    changeOrderparentNodes.Add(new TreeNode(changeOrderDesignItemShopStatusExtended, changeOrderimgKey, changeOrderimgKey)
                    {
                        Name = "Shops Drawing Status",
                        ToolTipText = "Shops Drawing Status",
                        Tag = changeOrderDesignItemShopsStatus.Value
                    });

                    if (changeOrderDesignItemRunCount.Value == 0 && changeOrderDesignItemRunUnConCount.Value > 0)
                    {
                        changeOrderparentNodes.Add(new TreeNode("Unconsolidated Runs: " + changeOrderDesignItemRunUnConCount.Value)
                        {
                            Name = "Unconsolidated Runs",
                            ToolTipText = "Unconsolidated Runs",
                            Tag = changeOrderDesignItemRunUnConCount.Value
                        });
                    }
                    else
                    {
                        changeOrderparentNodes.Add(new TreeNode("Run Count: " + changeOrderDesignItemRunCount.Value)
                        {
                            Name = "Run Count",
                            ToolTipText = "Run Count",
                            Tag = changeOrderDesignItemRunCount.Value
                        });
                    }

                    this.LoadChangeOrderNodeIcon(parent.Nodes[indexProject].Nodes[iDesign].Nodes[tn.Index], iChangeOrderDesign, changeOrderDesignItemStatusKey.Value, changeOrderDesignItemIsLocked.Value);
                }

                iDesign++;
            }
        }

更新

为了执行更简单的方法,我进行了递归并根据收到的项目设置变量:

private void AssignDesign(DataTable designItemList, DataTable designFolioList, TreeNode parent, string designName, string legacyKey, int indexProject, string DesignType, string Condition, int currDesign)
        {
            var iDesign = 0;


            foreach (var currentDesignItem in designItemList.Select($"[Design Name] = '{designName}' AND [Project Number] = {legacyKey}  AND [ParentDesignKey] {Condition}"))
            {
                var designItemName = currentDesignItem["DesignItemName"] as string;
                var designItemKey = currentDesignItem["DesignKey"] as int?;
                var designItemStatus = currentDesignItem["Design Status"] as string;
                var designItemIsLocked = currentDesignItem["IsLocked"] as bool?;
                var designItemStatusKey = currentDesignItem["StatusKey"] as int?;
                var designItemCreated = currentDesignItem["Design Created"] as DateTime?;
                var designItemRunCount = currentDesignItem["RunCount"] as int?;
                var designItemRunUnConCount = currentDesignItem["RunUnconCnt"] as int?;
                var designItemShopsStatus = currentDesignItem["ShopsStatus"] as int?;
                var designItemShopStatusExtended = currentDesignItem["ShopStatusExtended"] as string;
                var designItemFolio = designFolioList.Select($"[DesignKey] ={designItemKey}").FirstOrDefault()["Folio"].ToString();
                var changeOrderParentDesignKey = currentDesignItem["ParentDesignKey"] as int?;
                //var designConfirmDeliveryDate = currentDesignItem["ConfirmDeliveryDate"] as DateTime?;

                if (designItemName is null || designItemShopsStatus is null || designItemShopStatusExtended is null || designItemKey is null || designItemStatus is null || designItemStatusKey is null || designItemCreated is null || designItemRunCount is null || designItemRunUnConCount is null || designItemIsLocked is null) continue;



                //Create lowest level and assign their tree nodes
                var projectIndex = parent.Index;
                TreeNodeCollection parentNodes;
                if (DesignType == "BDCD")
                {
                    parent.Nodes[indexProject].Nodes.Add(designItemName);
                    parent.Nodes[indexProject].Nodes[iDesign].Tag = $"{designItemKey.Value}|{projectIndex}|BDCD";
                    parent.Nodes[indexProject].Nodes[iDesign].Name = "Design";
                    parent.Nodes[indexProject].Nodes[iDesign].ToolTipText = $"Design: {designItemName} - Folio: {designItemFolio}";
                    parent.Nodes[indexProject].Nodes[iDesign].ContextMenuStrip = contextMenuDesign;
                    this.LoadDesignStatusNodeIcon(parent.Nodes[indexProject].Nodes, iDesign, designItemStatusKey.Value, designItemIsLocked.Value);
                    parentNodes = parent.Nodes[indexProject].Nodes[iDesign].Nodes;

                }
                else
                {
                    TreeNode tn = parent.Nodes[indexProject].Nodes[currDesign].Nodes.Add(designItemName);
                    parent.Nodes[indexProject].Nodes[currDesign].Nodes[tn.Index].Tag = $"{designItemKey.Value}|{projectIndex}|CO";
                    parent.Nodes[indexProject].Nodes[currDesign].Nodes[tn.Index].Name = "Design";
                    parent.Nodes[indexProject].Nodes[currDesign].Nodes[tn.Index].ToolTipText = $"Change Order: {designItemName} - Folio: {designItemFolio}";
                    parent.Nodes[indexProject].Nodes[currDesign].Nodes[tn.Index].ContextMenuStrip = contextMenuDesign;
                    this.LoadChangeOrderNodeIcon(parent.Nodes[indexProject].Nodes[currDesign].Nodes[tn.Index], currDesign, designItemStatusKey.Value, designItemIsLocked.Value);
                    parentNodes = parent.Nodes[indexProject].Nodes[currDesign].LastNode.Nodes;
                }


                //load icon for design item
                var dateName = string.Empty;
                switch (DesignType)
                {
                    case "BDCD":
                        dateName = "Design Created";
                        break;
                    case "CO":
                        dateName = "Change Order Design Created";
                        break;


                }

                parentNodes.Add(new TreeNode($"{designItemCreated.Value.ToLongDateString()} ({designItemCreated.Value.ToString("yyMMdd")})", 8, 8)
                {
                    Name = dateName,
                    ToolTipText = "Created Date"
                });
                parentNodes.Add(new TreeNode(designItemStatus, 18, 18)
                {
                    Name = "Design Status",
                    ToolTipText = "Design Status",
                    Tag = $"{designItemStatusKey.Value}|{designItemIsLocked}"
                });
                var imgKey = LoadShopsNodeIcon(designItemShopsStatus.Value);

                parentNodes.Add(new TreeNode(designItemShopStatusExtended, imgKey, imgKey)
                {
                    Name = "Shops Drawing Status",
                    ToolTipText = "Shops Drawing Status",
                    Tag = designItemShopsStatus.Value
                });

                if (designItemRunCount.Value == 0 && designItemRunUnConCount.Value > 0)
                {
                    parentNodes.Add(new TreeNode("Unconsolidated Runs: " + designItemRunUnConCount.Value)
                    {
                        Name = "Unconsolidated Runs",
                        ToolTipText = "Unconsolidated Runs",
                        Tag = designItemRunUnConCount.Value
                    });
                }
                else
                {
                    parentNodes.Add(new TreeNode("Run Count: " + designItemRunCount.Value)
                    {
                        Name = "Run Count",
                        ToolTipText = "Run Count",
                        Tag = designItemRunCount.Value
                    });
                }

                var lastItem = string.Empty;
                foreach (var item in designItemList.Select($"[Design Name] = '{designName}' AND [Project Number] = {legacyKey} AND [ParentDesignKey] = {designItemKey}"))
                {
                    if (lastItem != parent.Text)
                    {
                    AssignDesign(designItemList, designFolioList, parent, designName, legacyKey, indexProject, "CO", $"= {designItemKey}", iDesign);
                    }
                    lastItem = parent.Text;
                }

                iDesign++;



            }
        }

但是您可以看到我在方法递归中发送查询过滤器:

第一个foreach:

AssignDesign(tvTable, designFolioList, parent, designName, currentProjectNumber, iDesign, "BDCD","IS NULL", 0);

第二次foreach(递归)

 AssignDesign(designItemList, designFolioList, parent, designName, legacyKey, indexProject, "CO", $"= {designItemKey}", iDesign);

我首先发送"IS NULL" 第二个$"= {designItemKey}"

有什么方法可以只使用一次查询,而不是两次使用不同的过滤器查询吗?

0 个答案:

没有答案