循环中比较两个哈希表的值

发布于 2024-12-21 15:45:17 字数 2011 浏览 2 评论 0原文

我有两个哈希表。我想根据键比较两个哈希表的值。我想在循环中执行此操作,如果找到匹配则想要执行字符串构建操作。但问题是我不知道有什么机制可以在循环中比较它们。请指导我... 以下是我要比较的哈希表

       HashTable OldTable= new HashTable();

        OldTable.Add("Date of Event", OCEFData.EventDate);
            OldTable.Add("Angina Status", OCEFData.AnginaStatusValue);
            OldTable.Add("Please indicate the body system involved (tick all that apply)",strBodySystem.ToString());
            OldTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
            OldTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
            OldTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
            OldTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
            OldTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
            OldTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
            OldTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);



       HashTable NewTable= new HashTable();

       NewTable.Add("Date of Event", OCEFData.EventDate);
        NewTable.Add("Angina Status", OCEFData.AnginaStatusValue);
        NewTable.Add("Please indicate the body system involved (tick all that apply)", strBodySystem.ToString());
        NewTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
        NewTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
        NewTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
        NewTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
        NewTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
        NewTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
        NewTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);

I have two hash tables. I want to compare values of both the hash tables based on the key. I want to do this in loop and if match is found is want to perform string building operation. But the problem is I dont know any mechanism to compare them in loop. Please guide me...
Following are my hash tables to be compared

       HashTable OldTable= new HashTable();

        OldTable.Add("Date of Event", OCEFData.EventDate);
            OldTable.Add("Angina Status", OCEFData.AnginaStatusValue);
            OldTable.Add("Please indicate the body system involved (tick all that apply)",strBodySystem.ToString());
            OldTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
            OldTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
            OldTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
            OldTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
            OldTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
            OldTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
            OldTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);



       HashTable NewTable= new HashTable();

       NewTable.Add("Date of Event", OCEFData.EventDate);
        NewTable.Add("Angina Status", OCEFData.AnginaStatusValue);
        NewTable.Add("Please indicate the body system involved (tick all that apply)", strBodySystem.ToString());
        NewTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
        NewTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
        NewTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
        NewTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
        NewTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
        NewTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
        NewTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉酒的小男人 2024-12-28 15:45:17

这是你想要的方式吗?

Hashtable OldTable = new Hashtable();
Hashtable NewTable = new Hashtable();

        foreach (DictionaryEntry entry in OldTable)
        {
            if(NewTable.ContainsKey(entry.Key))
            {
                //Do something?
            }
        }

Is this the way you want it?

Hashtable OldTable = new Hashtable();
Hashtable NewTable = new Hashtable();

        foreach (DictionaryEntry entry in OldTable)
        {
            if(NewTable.ContainsKey(entry.Key))
            {
                //Do something?
            }
        }
下壹個目標 2024-12-28 15:45:17

假设您正在尝试匹配两个 HashTable 中的键...您可以这样做

 foreach (string key in oldtable.Keys)
    {
        if (newtable.Contains(key))
        {
            // do your work;
        }
        else
        {
            // do your work;         
       }
    }

Assuming that you are trying to match keys in both HashTable ... you can do like

 foreach (string key in oldtable.Keys)
    {
        if (newtable.Contains(key))
        {
            // do your work;
        }
        else
        {
            // do your work;         
       }
    }
吃颗糖壮壮胆 2024-12-28 15:45:17

首先 - 使用类型化的 Dictionary 而不是 Hashtable,下面是值是 string 类型时的示例,这很容易将 string 更改为您的自定义类型(我相信 enum 或已经是字符串常量)。

IDictionary<string, string> map1 = new Dictionary<string, string>
                                        {
                                            {"A", "M1-A"},
                                            {"B", "M1-B"},
                                            {"C", "M1-C"}
                                        };

IDictionary<string, string> map2 = new Dictionary<string, string>
                                        {
                                            {"A", "M2-A"},
                                            {"B", "M2-B"},
                                            {"D", "M2-D"}
                                        };

.NET 3.5 及更高版本:Usign LINQ Intersect()

var items = map1.Keys.Intersect(map2.Keys)
                     .Select(k => map1[k] + " / " + map2[k])
                     .ToList();

<.NET 3.5

IList<string> results = new List<string>();
foreach (var key in map1.Keys)
{
    if (map2.ContainsKey(key))
    {
        results.Add(map1[key] + " / " + map2[key]);
    }
}

OUTPUT:
M1-A / M2-A
M1-B / M2-B

First thing - use typed Dictionary<TKey, TValue> rather than Hashtable, below is example when values are of string type, this would be easy changing string to your custom type (I believe enum or already string constants).

IDictionary<string, string> map1 = new Dictionary<string, string>
                                        {
                                            {"A", "M1-A"},
                                            {"B", "M1-B"},
                                            {"C", "M1-C"}
                                        };

IDictionary<string, string> map2 = new Dictionary<string, string>
                                        {
                                            {"A", "M2-A"},
                                            {"B", "M2-B"},
                                            {"D", "M2-D"}
                                        };

.NET 3.5 and upper: Usign LINQ Intersect()

var items = map1.Keys.Intersect(map2.Keys)
                     .Select(k => map1[k] + " / " + map2[k])
                     .ToList();

<.NET 3.5

IList<string> results = new List<string>();
foreach (var key in map1.Keys)
{
    if (map2.ContainsKey(key))
    {
        results.Add(map1[key] + " / " + map2[key]);
    }
}

OUTPUT:
M1-A / M2-A
M1-B / M2-B
思念绕指尖 2024-12-28 15:45:17

您可以使用 Linq 来交叉键,从而获得两个表中的键集合?

var combinedKeys=OldTable.Keys.Cast<string>().Intersect(NewTable.Keys.Cast<string>())

然后,您可以迭代这些键或使用 Linq Select 或 Agregate 语句来获取集合结果。

编辑

由于 HashTable 不是强类型,Keys 不会为您提供 IEnumerable,因此调用 Cast() 来获取IEnumerable

如果您使用强类型 Dictionary 您将不需要Cast() 部分。

You could use Linq to intersect the keys giving you a collection of keys in both tables?

var combinedKeys=OldTable.Keys.Cast<string>().Intersect(NewTable.Keys.Cast<string>())

you can then iterate over the keys or use a Linq Select or Agregate statement to get a collection result.

EDIT

Because HashTable is not strongly typed, Keys does not give you an IEnumerable, hence the call to Cast<string>() to get an IEnumerable<string>

If you were using a strongly typed Dictionary<string,string> you would not need the Cast<string>() part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文