Copy nk / v pairs from Hashtable
I have a hash table with n number of records. I need to copy records between x and y and repeat them.
How can I do it?
Example:
HT1.Count = 500;
HT2 = HT1 [0] - HT1 [100];
- edit -
Just so you know, the reasoning behind this is that I'm creating PDF from .MSG files. The problem is that the end user getting a 12GB PDF causes problems: P
I need to split a PDF into 250 posts and then start a new PDF. So the order doesn't really matter, and it doesn't return the same records every time, as it will only be done once.
a source to share
My bet would be doing a custom function like this:
public IEnumerable<object> GetRange(Hashtable ht, int min, int max) {
int i = 0;
foreach (var key in ht.Keys) {
i++;
if (i > max) {
yield break;
}
if (i >= min) {
yield return ht[key];
} else continue;
}
}
However, please note that the order of the keys is not guaranteed, so this may have an unordered sequence of objects
a source to share
As others have stated, you cannot iterate over a hashtable the way you think it is. If, on the other hand, what you want is the result when the keys are in that range, you can do something like this:
public IDictionary<int, T> GetRange<T>(
IDictionary<int, T> source, int min, int max)
{
// add error checking for min,max, null, etc...
int capacity = Math.Max(0, max - min);
Dictionary<int, T> target = new Dictionary<int, T>(capacity);
for (int key = min; key < max; key++)
{
if (source.ContainsKey(key))
{
target.Add(key, source[key]);
}
}
return target;
}
Note that Hashtable
I am using the generic version ( Dictionary
) instead of the old one , but the idea is the same.
a source to share