Declaring and adding rows to a dictionary dynamically using c#

I have certain case types in a List.  I have to declare a Dictionary for each case type in the list and then add rows to the dictionary. How can I do it dynamically?

example: the list has the following case types: 'Blood, Summary, Bone, Tissue"
How do I do the following so that it dynamically creates a Dictionary for each case type:
Dictionary<string, DateTime> BloodReport = new Dictionary<string, DateTime>();
Dictionary<string, DateTime> SummaryReport = new Dictionary<string, DateTime>();
Dictionary<string, DateTime> BoneReport = new Dictionary<string, DateTime>();
Dictionary<string, DateTime> TissueReport = new Dictionary<string, DateTime>();

Open in new window


Then the case number list is like this:
Bone1234, Blood1243, Bone2345, Summary1234

And I want to add those cases to the respective Dictionary. Normal code will be like this, but how can I make it dynamic.
if (casenum.StartsWith("Blood", true, ci))
                    { BloodReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Bone", true, ci))
                    { BoneReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Tissue", true, ci))
                    { TissueReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Summary", true, ci))
                    { SummaryReports.Add(casenum, CompletedDate); }

Open in new window


Thanks for your help in advance.
patd1Asked:
Who is Participating?
 
mrjoltcolaConnect With a Mentor Commented:
Something like this, assuming you have a list of case types already (as you said).

var caseTypeDictionary = new Dictionary<string, Dictionary<string, DateTime>>();

foreach(var caseType in caseTypeList) {
   caseTypeDictionary.Add(caseType.Name, new Dictionary<string,DateTime>());
}
0
 
patd1Author Commented:
I understand this is adding dictionary as an item in the caseTypeDictionary. Now how do I add items to these dictionaries from the case number list.
if (casenum.StartsWith("Blood", true, ci))
                    { BloodReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Bone", true, ci))
                    { BoneReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Tissue", true, ci))
                    { TissueReports.Add(casenum, CompletedDate); }
                    else if (casenum.StartsWith("Summary", true, ci))
                    { SummaryReports.Add(casenum, CompletedDate); }

Open in new window


Thanks.
0
 
mrjoltcolaConnect With a Mentor Commented:
if(casenum.StartsWith("Blood", true, ci))
   caseTypeDictionary["Blood"].Add(casenum, CompletedDate);
else if(...)
etc.

But it would be better if your case record actually had the case type in it (rather than "Blood123" it had "Blood"), because you could replace all of the if/then/elses with one add:

   caseTypeDictionary[case.caseType].Add(caes.casenum, case.CompletedDate);

0
 
patd1Author Commented:
But it would be better if your case record actually had the case type in it (rather than "Blood123" it had "Blood"), because you could replace all of the if/then/elses with one add:

I may have multiple Blood case numbers in my case number list (Blood123, Blood 234 etc)

The code that you provided above makes sense to me. I will definitely try this out ASAP.

Thanks for you help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.