c# – preventing the item to add to the dictionary

In addition to this question:

I want to create a hierarchical list in a 3 level category.

My data are in the following ways:

var appCategoryList = new List<AppCategoryDataModel>() {
    new AppCategoryDataModel() { AppCategoryId = 4844, ParentappCategoryId = null , AppCategoryName = "ItemResearch", ProtocolId = 5164, ProtocolTitle="ABC: Evaluation" },
    new AppCategoryDataModel() { AppCategoryId = 4844, ParentappCategoryId = null, AppCategoryName = "ItemResearch", ProtocolId = null, ProtocolTitle=""},
    new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId = null, ProtocolTitle="" },
    new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5162 , ProtocolTitle="ABC: 3/28/20" },
    new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation" },
    new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5165 , ProtocolTitle="ABC: section", },
    new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5192 , ProtocolTitle="ABC: 3/31/20", },
    new AppCategoryDataModel() { AppCategoryId = 4846, ParentappCategoryId = null, AppCategoryName = "Contact information", ProtocolId = null, ProtocolTitle="", },
    new AppCategoryDataModel() { AppCategoryId = 4846, ParentappCategoryId = null, AppCategoryName = "Contact information", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation", },
    new AppCategoryDataModel() { AppCategoryId = 4852, ParentappCategoryId = null, AppCategoryName = "UP", ProtocolId =null , ProtocolTitle="", },
    new AppCategoryDataModel() { AppCategoryId = 4852, ParentappCategoryId = null, AppCategoryName = "UP", ProtocolId = 5164, ProtocolTitle="ABC: Evaluation", },
    new AppCategoryDataModel() { AppCategoryId = 5023, ParentappCategoryId = null, AppCategoryName = "Call survival guide", ProtocolId = null, ProtocolTitle="" },
    new AppCategoryDataModel() { AppCategoryId = 5023, ParentappCategoryId = null, AppCategoryName = "Call survival guides", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation" },
    new AppCategoryDataModel() { AppCategoryId = 5085, ParentappCategoryId = null, AppCategoryName = "OE", ProtocolId =null, ProtocolTitle="" },
    new AppCategoryDataModel() { AppCategoryId = 5085, ParentappCategoryId = null, AppCategoryName = "OE", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation" },

   new AppCategoryDataModel() { AppCategoryId = 5099, ParentappCategoryId = null, AppCategoryName = "OEM", ProtocolId =null , ProtocolTitle=null },

    new AppCategoryDataModel() { AppCategoryId = 5334, ParentappCategoryId = 5085, AppCategoryName = "mmkk update", ProtocolId =null , ProtocolTitle= null },
    new AppCategoryDataModel() { AppCategoryId = 5336, ParentappCategoryId = 5085, AppCategoryName = "xdgdrg", ProtocolId =null , ProtocolTitle=null },
    new AppCategoryDataModel() { AppCategoryId = 5348, ParentappCategoryId = 5023, AppCategoryName = "test", ProtocolId =null , ProtocolTitle=null },
    new AppCategoryDataModel() { AppCategoryId = 5341, ParentappCategoryId = 5023, AppCategoryName = "New Category Level-1", ProtocolId =null , ProtocolTitle= null },

    new AppCategoryDataModel() { AppCategoryId = 5349, ParentappCategoryId = 5341, AppCategoryName = "New Category Level-2", ProtocolId =null , ProtocolTitle= null },

    new AppCategoryDataModel() { AppCategoryId = 5352, ParentappCategoryId = 5348, AppCategoryName = "New category3", ProtocolId =null , ProtocolTitle= null },



}.OrderBy(ap => ap.AppCategoryName);

AppCategoryDataModel

public class AppCategoryDataModel
{
    public int AppCategoryId { get; set; }
    public int? ParentappCategoryId { get; set; }
    public string AppCategoryName { get; set; }
    public int? ProtocolId { get; set; }
    public string ProtocolTitle { get; set; }
}

Dictionary<int, FirstLevelCategory> dictCategories = new Dictionary<int, FirstLevelCategory>();

foreach (AppCategoryDataModel category in appCategoryList)
{
    if (!dictCategories.ContainsKey(category.AppCategoryId))
    {
        dictCategories.Add(category.AppCategoryId, new FirstLevelCategory()
        {
            AppCategoryId = category.AppCategoryId,
            AppCategoryName = category.AppCategoryName,
            AppCategoryProtocolData = category.ProtocolId != null ?

            new List<ProtocolDetails>() {
                new ProtocolDetails()
                {
                    ProtocolId = (int)category.ProtocolId,
                    ProtocolTitle = category.ProtocolTitle
                }
            } : new List<ProtocolDetails>(),

            SubCategory = category.ParentappCategoryId != null
            ? GetSubCategoryList(appCategoryList, category.AppCategoryId)
            : new List<SecondLevelCategory>()
        });
    }
    else
    {
        if (category.ProtocolId != null)
            dictCategories[category.AppCategoryId].AppCategoryProtocolData.Add(new ProtocolDetails()
            {
                ProtocolId = (int)category.ProtocolId,
                ProtocolTitle = category.ProtocolTitle
            });
    }
}
foreach (var key in dictCategories.Keys.ToList())
{
    dictCategories[key].CategoryProtocolCount = dictCategories[key].AppCategoryProtocolData.Count();
}

FirstLevelCategory

public class FirstLevelCategory
{
    public int AppCategoryId { get; set; }
    public string AppCategoryName { get; set; }
    public int CategoryProtocolCount { get; set; }
    public List<SecondLevelCategory> SubCategory { get; set; }
    public List<ProtocolDetails> AppCategoryProtocolData { get; set; }
}
public class ProtocolDetails
{
    public int ProtocolId { get; set; }
    public string ProtocolTitle { get; set; }
}

My problem here is the item where ParentappCategoryId != null should come as a sub-category, but that has also been added in the first level category. I am not able to filter that properly.

How should I prevent the item that has ParentAppCategory to be added in the first.

Expected Result:

   ItemResearch
        ABC: Evaluation
    ItemHostpital
        ABC: 3/28/20
        ABC: Evaluation
        ABC: section
        ABC: 3/31/20
    Contact information
        ABC: Evaluation
    UP
        ABC: Evaluation
    OE
        ABC: Evaluation
            mmkk update --subcategory
            xdgdrg      --subcategory
    Call survival guides
        ABC: Evaluation --details
            test  --subcategory
                New category3 --thirdcategory
            New Category Level-1 --subcategory
                New Category Level-2 -- thirdcategory
 

Leave a Comment