2016-10-08 43 views
0

我有以下的解釋的:C#確定是否詞典包含所有的一組值

Dictionary<string, ArrayList> vertices = new Dictionary<string, ArrayList>(); 
vertices.Add("Key1",stringarray1); 
vertices.Add("Key2",stringarray2); 
vertices.Add("Key3",stringarray3); 

現在我想弄清楚是如何檢查每個字典的其它鍵(如"Key2"或如果它們的值包含"Key1"的值中的一個或全部值(ArrayList),但它不起作用。這可能是非常簡單的,但我無法得到它

var values = (ArrayList)vertices["Key1"]; 
foreach (var val in vertices) 
{ 
    if (val.Key != "Key1" && val.Value.Contains(values)) 
    { 
     //do something here 
    } 
} 
+0

不知道這是你的問題,但默認字符串比較是區分大小寫的。使用'val.Key!=「Key1」&&' – RyBolt

+0

你存儲在arrayList中的數據是什麼?你使用這種結構的具體原因是什麼? –

回答

1

的問題是要傳遞錯誤的東西到ContainsContains應該收到要在集合中查找的項目,但是您要傳遞整個集合。由於使用的是ArrayList,那裏的object您沒有收到一個編譯時錯誤的商品存儲,但它只是不工作(和項目,採用object比較了的比較哪個檢查引用。

你可以使用LINQ做這種方式:

string key = "Key1"; 
var key1Collection = vertices[key].Cast<object>().ToList(); 
foreach(var item in vertices.Where(x => x.Key != key)) 
{ 
    //If you want that all the items of the collection will be in the "Key1" collection: 
    if(item.Value.Cast<object>().All(x => key1Collection.Contains(x)) 
    { 
     //Do stuff 
    } 

    //Or if you want that at least 1 of the items of the collection will be in the "Key1" collection: 
    if(item.Value.Cast<object>().Any(x => key1Collection.Contains(x)) 
    { 
     //Do stuff 
    } 
} 

如果你改變你的數據結構從ArrayListList<TheTypeOfYourItems>那麼你並不需要所有的.Cast<object>

+0

我需要找出哪個確切的鍵返回true,哪些不是,因爲我需要做一些手動操作。你能幫忙嗎? – Srini

+0

@Srini - 查看更新 –

+0

@Srini - 你還在使用ArrayList的原因是什麼?你放在那裏的實際數據是什麼? –

1

你不能傳遞一個集合到包含方法,而是一個單一的元素。所以你需要迭代key1數組中的元素並檢查另一個數組是否也包含它。

var key1Val = vertices["key1"]; 
foreach (var val in vertices) 
     { 
      if(val.Key != "key1") 
      { 
       bool exist = false; 
       foreach (var element in val.Value) { 
         if(key1Val.Contains(element)){ 
          exist = true; 
          break; 
         } 
       } 
        if(exist){  /*do stuff*/} 
       //do something here 
      } 
     } 
+0

這可行,但非常蠻力的方法 – Srini

+1

你標記爲有幫助的答案具有與我的答案完全相同的時間複雜性,所以我有點困惑,你怎麼認爲它可以變得更有效率。我承認它使用了更簡潔的語法(我試圖合併你寫的內容),但不要讓它讓你覺得它在執行過程中更快。 –

+1

最糟糕的情況是,您總是需要將非key1列表中的每個元素與key1列表中的所有元素進行比較。你可以做的唯一的優化就是當你打一場比賽(這就是我所做的)時打破檢查。使用Linq符號不會改變這個事實。 –

1
var key1Values; 
if (!vertices.TryGetValue("Key1", out key1Values)) { 
    return; 
} 

foreach(KeyValuePair<string, ArrayList> entry in vertices) 
{ 
    if((entry.Key == "Key2" || entry.Key == "Key3") && entry.Value.Any(item => key1Values.Contains(item)) 
    { 
     //do some work 
    } 
} 
1

這個怎麼樣:

var keys = 
    vertices 
     .Where(y => y.Key != "Key1") 
     .Where(y => vertices["Key1"].Cast<string>().Intersect(y.Value.Cast<string>()).Any()) 
     .Select(x => x.Key); 

或者,如果你改變ArrayListList<string>:如果

var keys = 
    vertices 
     .Where(y => y.Key != "Key1") 
     .Where(y => vertices["Key1"].Intersect(y.Value).Any()) 
     .Select(x => x.Key); 
+0

糾正我,如果我錯了,但不是你比較字典中的每個列表與其他列表嗎? OP只是要求將第一個列表與所有其他列表進行比較。例如,我們不想找到列表2和列表3的交集,因爲這是不必要的。 –

+0

而且它會導致重複的匹配,而1 - 2交點是相同的2-1交集。 –

+0

@ LucasKot-Zaniewski - 你是對的。我已經解決了答案。 – Enigmativity

相關問題