Search code examples
c#asp.netvisual-studiosql-server-2008sortedlist

How to add Sorted List within another Sorted List in C#?


I am getting this error because I have a duplicate key in the SortedList.

Item has already been added. Key in dictionary: 'V22.1' Key being added: 'V22.1'

BUT the value of these duplicate keys are different. So I am thinking of adding another object before the preceding the duplicate key in order to find its value. I was thinking of putting SortedList within a SortedList. An illustration for my intention for example:

(key)"ICD9" : (key)"V22.1" : (value)"Supervision of other normal pregnancy"
(key)"ICD10" : (key)"V22.1" : (value)"Motorcycle passenger injured in collision with two- or three-wheeled motor vehicle in nontraffic accident"

I hope that makes sense. I was thinking of doing something like this:

    SortedList<string, SortedList<string, string>> slCodes;
    slCodes = new SortedList<string, SortedList<string, string>>();

But the part I am stuck now is how do I add into the SortedList within the SortedList? I am stuck here:

    strDesc = tbDesc.Text.Trim();
    tblCodes = new DataTable();
    GetCodesByDescription(strDesc, ref tblCodes); //<--This queries from database
    DataView dvCodes = new DataView(tblCodes);

    dvCodes.RowFilter = "CodeType='ICD10' OR CodeType='ICD9'";
    foreach(DataRowView drv in dvCodes)
    {
        slCodes.Add(drv["Code"].ToString().Trim(), //<--Throws error here.
            drv["Description"].ToString().Trim());
    }

This is currently where I am stuck at on adding into the SortedList within a SortedList. I'm not even sure if this approach to having a key-value pair within a key is correct. Please help.


Solution

  • You need to first check the code type to determine which sub list to add to and if you need to create a new sub list.

    foreach(DataRowView drv in dvCodes)
    {
        var codeType = drv["CodeType"].ToString().Trim();
        var code = drv["Code"].ToString().Trim();
        var description = drv["Description"].ToString().Trim();
        if(slCodes.ContainsKey(codeType))
        {
            slCodes[codeType].Add(code, description);
        }
        else
        {
            var subList = new SortedList<string, string>();
            subList.Add(code, description);
            slCodes.Add(codeType, subList);
        }
    }
    

    Alternatively since you only pull ICD9 and ICD10 codes you could prepopulate the two sub lists

    var slCodes = new SortedList<string, SortedList<string, string>>();
    slCodes.Add("ICD9", new SortedList<string, string>());
    slCodes.Add("ICD10", new SortedList<string, string>());
    
    dvCodes.RowFilter = "CodeType='ICD10' OR CodeType='ICD9'";
    foreach(DataRowView drv in dvCodes)
    {
        var codeType = drv["CodeType"].ToString().Trim();
        var code = drv["Code"].ToString().Trim();
        var description = drv["Description"].ToString().Trim();
        slCodes[codeType].Add(code, description);
    }