2016-09-16 18 views
4

我目前有2個詞典,其中包含ard 30,000keys。使用foreach和.contains比較兩個詞典以找到唯一和非唯一數據的最快方法

我目前使用兩個foreach循環來查找dict1中的唯一鍵。然後我將dict2中的唯一鍵寫入另一個字典(3)。

如果鍵匹配我執行檢查,看看值是否相同。如果它們不一樣,就打印出來。

使用foreach是否更好,它似乎影響性能。有沒有其他更快的解決方案或內置函數? Ir shd我嘗試了dict.contains方法。


Dict1     Dict2 

Keys Values   Keys  Values   
S0111 00000   S0000 00010 
S0000 00010   S0020 00015 
S0020 00015   S0040 00150 
S0040 00200   S0050 00250 

 foreach (KeyValuePair<string, string> sourceRow in sourceData) 
     { 
      foreach (KeyValuePair<string, string> dumpRow in dumpData) 
      { 
       // A in C, skip 
       if (sourceRow.Key == dumpRow.Key) 
       { 
        Is_Unique_Address = false; 

        if (sourceRow.Value != dumpRow.Value) 
        { 
         Data_Mismatch_Criteria(sourceRow.Key, sourceRow.Value, dumpRow.Key, dumpRow.Value); 
        } 

       } 
      } 
     } 
+2

什麼你通過尋找獨特的按鍵的意思是,在字典中的鍵總是唯一的! – sachin

+0

目前尚不清楚。字典中的所有密鑰始終是唯一的。在此提供代碼示例。 –

+1

我認爲在這種情況下是唯一的手段:在字典中,但在其他字典中的鍵 – DarkSquirrel42

回答

2
foreach (KeyValuePair<string, string> sourceRow in sourceData) 
{ 
    string dumpValue; 

    if (dumpData.TryGetValue(sourceRow.Key, out dumpValue) && dumpValue != sourceRow.Value) 
    { 
     Data_Mismatch_Criteria(sourceRow.Key, sourceRow.Value, sourceRow.Key, dumpValue); 
    } 
} 
+0

看貨。 1qn。爲什麼我們使用dumpValue? 這比使用dumpData.Contains(sourceRow.key,out dumpValue)更快嗎? – Manick9

5

此代碼,您可以從dict2,這是不包含在dict1選擇鍵。

var dict1 = new Dictionary<string, string>(); 
var dict2 = new Dictionary<string, string>(); 

// Add data to both dictionaries 

var uniqueKeys = dict2.Keys.Except(dict1.Keys); 

這就是你需要的嗎?

編輯:請注意,上面的代碼選擇dict1中沒有包含的所有密鑰。如果您需要檢查兩個字典有不同的密鑰組(我認爲它可能的情況下),那麼你可以使用:

var areTheSame = dict2.Keys.Any(x => !dict1.ContainsKey(x)) 
|| dict1.Keys.Any(x => !dict2.ContainsKey(x)); 

編輯2:OP編輯後,我現在知道什麼您需要:

var differentValues = dict2.Where(pair => dict1.ContainsKey(pair.Key)) 
    .Select(pair => new 
    { 
     ValueA = pair.Value, 
     ValueB = dict1[pair.Key], 
     Key = pair.Key 
    }) 
    .Where(x => x.ValueA != x.ValueB) 
    .ToList(); 

foreach (var differentValue in differentValues) 
{ 
    Console.WriteLine(differentValue); 
} 
+0

我需要提取這些信息。鍵只在dict1中,鍵只在字典中2.鍵在dict1和dict 2中,但它們的值不匹配 – Manick9

+1

@ Manick9好吧,看看這是否合適。 –

1

那麼,這LINQ查詢將返回,要麼沒有在兩個字典呈現或記錄詞典有不同的值:

var dictOfUniqueValues = 
    dict1.Where(kv => !dict2.ContainsKey(kv.Key)).Concat(
     dict2.Where(kv => !dict1.ContainsKey(kv.Key) || dict1[kv.Key] != kv.Value)) 
    .ToDictionary(kv => kv.Key, kv => kv.Value); 

好了,讓我們怎麼在這裏就仔細看看:

var dictOfUniqueValues = 

    // get records from dict1 that don't exist in dict2 by comparing keys 
    dict1.Where(kv => !dict2.ContainsKey(kv.Key)).Concat(

    // get records from dict2 that don't exist in dict1 by comparing keys 
    // and records that do exist, but values are not equal 
     dict2.Where(kv => !dict1.ContainsKey(kv.Key) || dict1[kv.Key] != kv.Value)) 

    // convert the resulting IEnumerable<KeyValuePair<T1, T2>> to dictionary 
    .ToDictionary(kv => kv.Key, kv => kv.Value); 
+0

我覺得很難理解爲初學者。 – Manick9

相關問題