patterncsharpMinor
Iterating through a dictionary in C#
Viewed 0 times
dictionaryiteratingthrough
Problem
I am trying to get my dictionary to iterate seven times after the
I could not figure out how to iterate through the range I want, so instead I iterate through the entire dictionary and have a bunch of it statements to find the values I am looking for. Here is my code. I know I can use a
dateTime is found. I want to iterate through the next week of objects in my dictionary pagesInRange. I figured out how to do it, but I am sure this is not good code. I am wondering if someone could point me in the direction of what I can do to improve this code. I could not figure out how to iterate through the range I want, so instead I iterate through the entire dictionary and have a bunch of it statements to find the values I am looking for. Here is my code. I know I can use a
switch statement to improve the code, but I wondering if there is a way given the first value to iterate over the next seven values.Console.WriteLine("Please enter a date in the following formate MM/DD/YYYY");
string date = Console.ReadLine();
DateTime dt = Convert.ToDateTime(date);
DateTime end1 = dt.AddDays(1);
DateTime end2 = dt.AddDays(2);
DateTime end3 = dt.AddDays(3);
DateTime end4 = dt.AddDays(4);
DateTime end5 = dt.AddDays(5);
DateTime end6 = dt.AddDays(6);
int i = 0;
foreach (var pair in pagesInRange)
{
if (pair.Key.ToString("d")==date)
{
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);
} else if (pair.Key == end1)
{
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);
}
else if (pair.Key == end2)
{
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);
}
else if (pair.Key == end3)
{
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);
}
else if (pair.Key == end4)
{
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);
}
else if (pair.Key == end5)
{
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);
}
else if (pair.Key == end6)
{
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);
}
}Solution
If I understand your intent:
This does not take advantage of the fact that the dictionary is sorted.
Console.WriteLine("Please enter a date in the following formate MM/DD/YYYY");
string date = Console.ReadLine();
DateTime startRange = Convert.ToDateTime(date);
DateTime endRange = startRange.AddDays(6);
foreach (var pair in pagesInRange)
if (pair.Key >= startRange && pair.Key <= endRange)
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);This does not take advantage of the fact that the dictionary is sorted.
Code Snippets
Console.WriteLine("Please enter a date in the following formate MM/DD/YYYY");
string date = Console.ReadLine();
DateTime startRange = Convert.ToDateTime(date);
DateTime endRange = startRange.AddDays(6);
foreach (var pair in pagesInRange)
if (pair.Key >= startRange && pair.Key <= endRange)
Console.WriteLine(pair.Key.ToString("d") + " " + pair.Value);Context
StackExchange Code Review Q#7885, answer score: 5
Revisions (0)
No revisions yet.