如何在C#中加入8个哈希表? [英] How can I join 8 hash table in C# ?
本文介绍了如何在C#中加入8个哈希表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有八个哈希表ht1,ht2 ............. ht8所以我想在ht9中加载所有哈希表数据,这怎么可能。
I have eight hash table ht1,ht2.............ht8 so i want to load all hashtable data in a ht9 how can this possible.
推荐答案
请帮助我们,通过一个单线问题,你期待我们阅读你的想法,你的硬盘,你的屏幕和所有:)
虽然只是在黑暗中遇到解决方案就好了,你可以把你所有的哈希表h1..h8放在h9中如下
键h1 ,value => h1对象引用
Please help yourself by helping us, with a one liner question you are expecting us to read your mind, your hard disk, your screen and all :)
While just hitting in the dark a solution could be like, you can put all your hashtables h1..h8 in h9 as follows
Key "h1", value => h1 object reference
Hashtable h9 = new Hashtable();
h9.Add("h1", h1);
....
h9.Add("h8", h8);
以后您可以通过这样做获得h7对象
and later you can get h7 object by doing this
Hashtable tmpH7 = h9["h7"];
试试beow
Try like beow
var ht2 = new Hashtable {{"B", "b"}};
var ht3 = new Hashtable {{"A", "z"}};
var ht4 = new Hashtable {{"C", "c"}};
var l = new List<hashtable> {ht1, ht2, ht4};
var d = new Dictionary<object, object>();
d = l.Aggregate(d, (current, t) => current.Union(t.Cast<dictionaryentry>().ToDictionary(a => a.Key, a => a.Value)).ToDictionary(b => b.Key, b => b.Value));
希望这有帮助
Hope this helps
我可能会使用扩展方法来解决这个问题:
I might use the extension method to solve this problem:
public static class Helper
{
public static Hashtable Merger(this Hashtable ht, Hashtable ht1)
{
var e = ht1.GetEnumerator();
while (e.MoveNext())
{
if (!ht.ContainsKey(e.Key))
ht.Add(e.Key, e.Value);
}
return ht;
}
}
调用Merger()来合并哈希表:
Call Merger() to merger hash tables:
Hashtable ht1 = new Hashtable();
Hashtable ht2 = new Hashtable();
Hashtable ht3 = new Hashtable();
ht1.Add("a", 1);
ht1.Add("b", 2);
ht2.Add("c", 3);
ht3.Add("d", 4);
Hashtable ht4 = ht1.Merger(ht2).Merger(ht3);
这篇关于如何在C#中加入8个哈希表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文