-
September 18th, 2008, 02:01 AM
#1
foreach loop parallel
I need to loop two dictionary at the same time, like the for loop., how can i achieve this??
Code:
Dictionary<int,object> p_dictOld;
Dictionary<int,object> p_dictNew;
foreach (KeyValuePair<int, object> kvpOld in p_dictOld "&&" KeyValuePair<int, object> kvpONew in p_dictNew)
{
kvpOld.key = ...
kvpONew.key=...
........
}
-
September 18th, 2008, 03:07 AM
#2
Re: foreach loop parallel
This can only be done in FOR loop and the lengths should be equal for both dictionaries.
Correct me someone, if I am wrong.
-
September 18th, 2008, 04:09 AM
#3
Re: foreach loop parallel
How can this be done in FOR loop, because the key value pair can be anything, and i want to retrieve all the key value pair in the dictionaries.
-
September 18th, 2008, 08:10 AM
#4
Re: foreach loop parallel
how big do you suspect these dictionaries to get? if it's fairly small(less than 100), then you can just loop through them individually. if they are going to be huge(10,000+), then you probably need to look into multi-threading.
-
September 18th, 2008, 08:46 AM
#5
Re: foreach loop parallel
COuld you show me how to use FOR loop to do this, since the index or key can be anything. Also, let's say my dictionary is
Dicitonary<string, data>, how to use For loop to retrieve every pair of key, which is a string value and its corresponding data in the dictionary?
-
September 18th, 2008, 10:03 AM
#6
Re: foreach loop parallel
Code:
Dictionary<string, int> dictionary1 = new Dictionary<string, int>();
dictionary1.Add("1", 1);
dictionary1.Add("2", 2);
dictionary1.Add("3", 3);
Dictionary<int, short> dictionary2 = new Dictionary<int, short>();
dictionary2.Add(1, 1);
dictionary2.Add(2, 2);
dictionary2.Add(3, 3);
Dictionary<string, int>.Enumerator enumerator1 =
dictionary1.GetEnumerator();
Dictionary<int, short>.Enumerator enumerator2 =
dictionary2.GetEnumerator();
for (; ; )
{
// LOGIC HERE
if (!enumerator1.MoveNext() || !enumerator2.MoveNext())
{
break;
}
}
You could change the logic to break out of the loop depending on whether the dictionaries have the same number of keys, or whether you want to go through every entry in both dictionaries, or just the number of entries in the smaller dictionary (as shown above). It's implementation dependent, but I'm sure you can figure it out.
enumerator1.Current will give you the KeyValuePair for dictionary1.
-
September 18th, 2008, 12:10 PM
#7
Re: foreach loop parallel
you could also use a foreach loop.
Code:
Dictionary<string, string> dictionary1 = new Dictionary<string, string>();
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string,string> pair in dictionary1)
{
sb.Append(string.Format("{0}:{1}", pair.Key, pair.Value);
sb.Append(Environment.NewLine);
}
Console.WriteLine(sb.ToString()); // if you are doing a console app
MessageBox.Show(sb.ToString()); // if you are doing a windows app
-
September 18th, 2008, 12:21 PM
#8
Re: foreach loop parallel
No, you can't use a foreach loop effectively for the problem the OP is trying to solve.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|