2012-02-27 130 views
0

我有一個元組<Tuple<int, int>> x的列表,我得到的鍵和值爲(45345,1),(54645,0),(45345,0) 和I在一個Dictionary < int, string > PathList我得到鍵和值(45345,asdfsd234egsgdfgs56345),(54645,0dfsd234egsgdfgs563456),(45345,0dfsd234egsgdfgs56345234)如何比較字典鍵值和元組鍵值

我想

foreach (var item in PathList) 
    { 
     if (x.Equals(item.Key) && x[item.Key].Equals(0)) 
     { 
     string path1 = Path.Combine(GetDirectory(item.Value), item.Value); 
     File.Delete(path1); 
     } 
    } 

我想檢查的X id與id的PathList相同,x的值必須爲0,然後在條件內輸入......現在我在做的是在任何情況下,我無法進入ide If語句。

如何檢查我的病情?

讓我解釋更多:check this qus這是我返回一個元組列表,我得到(54356,0),(64643,0),(34365,1)在我的ascx頁面我有列表tuple <Tuple<int, int>> x,在這個xi中獲取列表的所有返回值,現在在同一個ascx頁面中我有Dictionary < int, string > PathList,因爲我添加了值ImgPathList.Add(54356,456dfhgdfg6575dfghdf);所以我得到了2個不同的列表,其中一個是x,另一個是Pathlist。

現在我想檢查。如果pathlist具有id和54356,x有54356和0,則在if語句中輸入else else顯示標記味精,因爲文件不能被刪除

+0

你能更清楚:究竟什麼是'X '?你使用'x.Equals(item.Key)'和'x [item.Key]'是很奇怪的。也;如果你在循環字典,通常意味着什麼是不正確的...... – 2012-02-27 07:53:35

+0

你的示例代碼中的x是什麼? – 2012-02-27 07:55:05

+0

如果你的'x'是'Tuple'的列表,那麼你就不能將'x'與'item.Key'進行比較,因爲'item.Key'是一個'int'。我在說('x.Equals(item.Key)') – 2012-02-27 07:55:36

回答

0

我已經修改我的代碼

foreach (var item in PathList) 
{ 
    Tuple<int, int> temp = new Tuple<int, int>(item.Key, 0); 
     //if (x.Equals(item.Key) && x[item.Key].Equals(0)) 
    if (x.Contains<Tuple<int, int>>(temp)) 
     { 
     string path1 = Path.Combine(GetDirectory(item.Value), item.Value); 
     File.Delete(path1); 
     } 
    } 

我不知道這是一個很好的方式或沒有,但它解決了我的問題......

0

將斷點放在條件循環中,直到找到一個值應該輸入if語句,然後使用「Watch」調試窗口查看錶達式的哪部分返回false。

+0

x.Equals(item.Key)返回false並且x [item.Key] .Equals(0)給出異常x [item.Key] .Equals(0)\t'x [item.Key]'拋出一個異常類型'System.ArgumentOutOfRangeException'\t bool – Rocky 2012-02-27 07:58:27

+0

@Rocky再次:究竟是什麼'x'? – 2012-02-27 08:00:01

+0

x是Tuple列表,其中id爲34234,65645,67567,每個id我的SP返回值都是0和1,因此它包含(34234,0),(65645,1) – Rocky 2012-02-27 08:04:19

0

這也許是有益的: 在我的項目,我有一本字典:

public Dictionary<int, double> CpuDictionary = new Dictionary<int, double>(); 

在某些時候,我嘗試使用,以找到一把鑰匙:

  int roundedcpupercentage = Convert.ToInt16(Math.Round(cpuusagepercentage)); 
      if (CpuDictionary.ContainsKey(roundedcpupercentage)) 
      { 
       temp.CPU = CpuDictionary[roundedcpupercentage]; 
       temp.Watt = temp.CPU; 
      } 

使用containsKey功能可以正常使用了我。也許你應該嘗試類似的東西。

2

我想了解的問題,但它聽起來就像我們,說:

var x = Tuple.Create(45345,0); 

在這種情況下,你只需要:

string value; 
if(PathList.TryGetValue(x.Item1, out value)) { 
    // there is an item in the dictionary with key 45345; 
    // the value is now in "value" 
} 

還有一些關於一零檢查;不知道你的意思,但也許只是檢查x.Item2

如果x實際上是一個清單,做一個循環:

foreach(var item in list) { 
    string value; 
    if(PathList.TryGetValue(item.Item1, out value)) { 
     // there is an element in the dictionary with key matching item; 
     // the value is now in "value" 
    } 
} 

可能是這哪裏是零檢查進來,太:

foreach(var item in list) { 
    string value; 
    if(item.Item2 == 0 && PathList.TryGetValue(item.Item1, out value)) { 
     // there is an element in the dictionary with key matching item; 
     // the value is now in "value" 
    } 
} 

然而,我無法充分理解你給出的例子,所以我不能肯定地說。

0

,如果你想檢查是否有與字典的關鍵一元組爲Item1Item2==0就是這樣:你上面有兩個相同的字典鑰匙在你的榜樣:

foreach(var item in pathList) { 
    var xItem = x.Find(i => i.Item1 == item.Key && i.Item2 == 0); 
    if(xItem =! null) { 
     // YourTuple.Item1==item.Key && YourTuple.Item2==0 => true 
    } 
} 

注意。

+1

循環訪問字典,然後使用列表中的「Find」查找每個鍵是實現此目的的一種非常低效的方式;即'O(N * M)'。在列表上循環對比('O(N)')並使用字典進行查找('O(1)') - 因此它是'O(N)'。 – 2012-02-27 08:19:20

+0

這已經足夠了。但是從問題中我們不清楚OP究竟在做什麼,所以我無法預見如何改進他的方法。因此,我只是將問題的標題作爲要求。 – 2012-02-27 08:27:19

+0

是的,這裏的背景是令人痛苦的混淆 - 同意。 – 2012-02-27 08:28:12