【问题标题】:How to compare the values in the dictionary with the new values?如何将字典中的值与新值进行比较?
【发布时间】:2013-08-13 11:33:11
【问题描述】:

我有一个字典变量,其中包含我从数据库中检索到的两个值。我需要更新数据库中的值,并且在更新时我需要检查更新的值是否存在于字典值中。我怎么能比较。到目前为止,我已经做了这么多。请帮我看看这是对还是错。

从数据库中检索数据并将其存储在字典变量“DictionaryName”中

For Each row As DataRow In ds.Tables(0).Rows
    DictionaryName.Add(row("EngMemID").ToString(), row("EngMemEmail").ToString())
Next

在此之后,我必须将它与我的新值进行比较。

        Dim pair As KeyValuePair(Of String, String)

        For Each pair In DictionaryName
            If DictionaryName.ContainsKey(newMember.EngMemID) Then
                ---Condition to compare the EmailID's 
                    NotifyOnUpdate(newMember)
                ---
            End If
        Next

比较 ContainsKey() 后,如何比较新旧电子邮件 ID。

请帮助我将字典中的这两个值与新值进行比较。

谢谢,

Udhaya S.

【问题讨论】:

  • newMember 对象从何而来?你在哪里比较你的 memberEmailList 字典和你的其他字典 DictionaryName
  • 对不起,这两个字典是一样的。我应该改名,但我没有。 “newMember”是来自我们更改值的另一个函数的更新数据。

标签: .net vb.net dictionary


【解决方案1】:

试试这个

 Dim oldValue As String
 If memberEmaillList.TryGetValue(EngMemID, oldValue) AndAlso oldValue <> EngMemEmail Then
        NotifyOnUpdate(newMember)
 End If

【讨论】:

    【解决方案2】:

    你不需要循环所有成员,你可以使用 ContainsKey

    If memberEmaillList.ContainsKey(newMember.EngMemID) Then
        engMemEmail = memberEmaillList(newMember.EngMemID)
    
        ' Key exists, engMemEmail is the value attached to the key
    Else
        ' Key does not exists
    End If
    

    【讨论】:

    • 我需要条件,因为它应该首先比较 Dictionary Key "engmemid" 如果相等,它必须进入下一个循环并将 Dictionary 值 "EmailID" 与更新的电子邮件 ID 进行比较。跨度>
    • @Udhay 您想将 EmailID 与哪个集合进行比较?我没有看到它存储在任何地方。你说的是 engMemEmail 吗?
    • 在一个函数中,我们有一个字典,我们将键和值保存为来自数据库的 EngMemID 和 EmailID。在另一个函数中,我们有 newmember.Engmemid 和 newmember.emailid,它们是更新后的值。首先,我需要将 Dictionary.key(EngMemID) 与 newmember.engmemid 进行比较,然后将 dictionary.value(EmailID) 与 newmember.email Id 进行比较。
    • 如果你存储的是 Dictionary(EngMemID, EmailID),那么 Dictionary(EngMemID) 返回的值就是 EmailID。在我的示例中,engMemEmail 应该是电子邮件 ID。
    • 知道了。,谢谢解释。,。
    【解决方案3】:

    这是 C#

    if(memberEmaillList.ContainsKey(newMember.EngMemID))
    {
         if(string.comare(memberEmaillList[newMember.EngMemID],newMember.EmailAddress,true) == 0))
         {
            //have match
         }
         else 
         {
            //no match
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多