我试图将我的三维词典填充到一个循环中,在其中循环浏览年,月和ItemCategories。
Dictionary<int,Dictionary<int, Dictionary<ItemCategories, List<ExpenseStatisticItem>>>> ExpenseItemsData
ExpenseItemsData[currentYear][currentMonth][category].Add(item);
在调用以下调试时:
ExpenseItemsData[2016][3][ItemCategory.Food].Add("Bla");
现在不知何故,它没有将条目放置在我告诉他的位置,而是放入了词典的每个条目中,类别==食物。不知何故,年月完全被忽略了。
我想念什么?
这是完整的代码:
string currentDate = "";
int currentMonth = 0;
int currentYear = 0;
string fileName = "DynamischeAusgaben.txt";
var directory = new DirectoryInfo("C:/MeineDaten/FinanzProgramm/Statistik/");
var years = directory.GetDirectories();
foreach (var year in years)
{
currentYear = int.Parse(year.Name);
if (currentYear == 0)
{
continue;
}
directory = new DirectoryInfo(String.Format("C:/MeineDaten/FinanzProgramm/Statistik/{0}/", currentYear));
var months = directory.GetDirectories();
foreach (var month in months)
{
currentMonth = int.Parse(month.Name);
if (currentMonth == 0)
{
continue;
}
int dayCount = DateTime.DaysInMonth(currentYear, currentMonth);
List<string> dayDates = new List<string>();
// Datums der verschiedenen Tage dieses Monats zusammenstellen
for (int i = 1; i < dayCount + 1; i++)
{
dayDates.Add(new DateTime(currentYear, currentMonth, i, 0, 0, 0).ToShortDateString());
}
string path = string.Format("C:/MeineDaten/FinanzProgramm/Statistik/{0}/{1}/DynamischeAusgaben",
currentYear, currentMonth);
bool fileExists = File.Exists(Path.Combine(path, fileName));
if (!fileExists)
{
continue;
}
string data = "";
using (StreamReader sr = new StreamReader(Path.Combine(path, fileName)))
{
data = sr.ReadToEnd();
data = data.Replace(System.Environment.NewLine, "");
}
foreach (var date in dayDates)
{
// Wenn das Datum nicht in der Datei enthalten ist, geh weiter.
if (!data.Contains(date))
{
continue;
}
string[] days = data.Split(';');
for (int i = 0; i < days.Length; i++)
{
// Leere Zeile ignorieren
if (days[i] == "")
{
continue;
}
string[] dayAndCategories = days[i].Split('<');
string dayString = dayAndCategories[0];
for (int j = 1; j < dayAndCategories.Length; j++)
{
string[] categoryAndItems = dayAndCategories[j].Split('|');
ItemCategories category = (ItemCategories)Enum.Parse(typeof(ItemCategories), categoryAndItems[0]);
decimal total = decimal.Parse(categoryAndItems[1]);
string[] itemsAndNothing = categoryAndItems[2].Split('>');
for (int k = 0; k < itemsAndNothing.Length; k++)
{
if (itemsAndNothing[k] == "")
{
continue;
}
string[] itemvalues = itemsAndNothing[1].Split('#');
ExpenseStatisticItem item = new ExpenseStatisticItem
{
Name = itemvalues[0],
Total = decimal.Parse(itemvalues[1]),
Count = int.Parse(itemvalues[2])
};
bool matchFound = false;
int matchIndex = 0;
// Ermitteln ob es schon ein Item mit diesem Namen in diesem Monat und Kategorie gibt
for (int l = 0; l < Global.ExpenseItemsData[currentYear][currentMonth][category].Count; l++)
{
ExpenseStatisticItem itemToCompare = Global.ExpenseItemsData[currentYear][currentMonth][category][l];
if (itemToCompare.Name == item.Name)
{
matchFound = true;
matchIndex = l;
}
}
// Wenn es schon ein Item gibt, dann fasse die Items zusammen
if (matchFound)
{
Global.ExpenseItemsData[currentYear][currentMonth][category][matchIndex].Count += item.Count;
Global.ExpenseItemsData[currentYear][currentMonth][category][matchIndex].Total += item.Total;
}
// Ansonsten erstelle einen neuen Eintrag
else
{
Global.ExpenseItemsData[currentYear][currentMonth][category].Add(item);
}
}
}
}
}
}
}