Reputation: 85
I have created a nested dictionary with the following:
Dictionary<DateTime, Dictionary<string, string>> dateDict =
new Dictionary<DateTime, Dictionary<string, string>>();
I then enter values to this dictionary using the following:
while (reader.Read())
{
DateTime date = DateTime.Parse(reader["Event_Date"].ToString());
string name = reader["Event_Name"].ToString();
string guid = reader["Event_GUID"].ToString();
dateDict.Add(date, new Dictionary<string, string>());
dateDict[date].Add(name, guid);
}
My question is how do I correctly pull the variables back out from the nested dictionary?
I have this loop running to pull values out and assign them to DateTimePickers and TextFields:
for (int i = 0; i < dateDict.Count; i++)
{
DateTime keyVar = dateDict.ElementAt(i).Key;
eventDateBoxes[i].Value = keyVar;
eventTextBoxes[i].Text = dateDict[keyVar[]];
}
The eventTextBoxes part is where im getting stuck on how to pull the value from the nested dictionary, hence why that part of the code is wrong/incomplete.
I can't use tuples as I am using .NET lower than 4 and can't seem to wrap my head around making a custom class to act as its own tuple or multidictionary.
Upvotes: 0
Views: 945
Reputation: 45145
It sounds like you don't need a Dictionary<DateTime, Dictionary<string, string>>
since you have only one item inside your inner dictionary, you need a Dictionary<DateTime, Dictionary<string, Event>>
where Event
is a class that holds you event data. Something like:
public class Event
{
public string Name {get; set;}
public string Guid {get; set;} // you could actually use the GUID type here!
}
Then you'd populate it like:
while (reader.Read())
{
DateTime date = DateTime.Parse(reader["Event_Date"].ToString());
string name = reader["Event_Name"].ToString();
string guid = reader["Event_GUID"].ToString();
dateDict.Add(date, new Event() { Name = name, Guid = guid };
}
Then when you get an item from your dictionary by key:
var item = dateDict[someKey];
You can get your name and guid back:
var name = item.Name;
var guid = item.guid;
Additionally, since you are iterating through your dictionary by numerical index rather than by key, it seems likely that you probably don't need a dictionary at all. Maybe all you need to do is add a Date
property to your Event
and just have an List<Event>
.
Upvotes: 1
Reputation: 28839
This is the basic outline of what you would do:
foreach (var element in dateDict)
{
eventDateBoxes[i].Value = element.Key;
foreach (var subElement in element.Value)
{
eventTextBoxes[i].Text = subElement.Value;
}
}
As others point out in the comment, there can be no one-to-one relationship between the elements in the outer dictionary and the text boxes as there can (potentially) be multiple entries in the sub-element.
Upvotes: 1
Reputation: 460158
I would use a foreach
or a LINQ query. But it's not clear what you want as DateTimePicker.Text
:
List<DateTimePicker> eventDateBoxes = dateDict
.Select(kv => new{
Date = kv.Key,
EventName = kv.Value["Event_Name"],
EventGuid = kv.Value["Event_GUID"],
})
.Select(x => new DateTimePicker{ Value = x.Date, Text = x.EventName })
.ToList();
Upvotes: 1