2012-10-11 124 views
-2

我有被內置了詞典,低於查詢字典,LINQ

Dictionary<string, string> GTNMobilelist = new Dictionary<string, string>(); 
GTNMobilelist = LoadMobileNumbers(); 

然後我需要查詢字典來檢查字典「中存在已enetered到一個文本框,一個手機號碼希望是有道理的」: -/

這是我

foreach (GridViewRow HandSetRow in grdvHandSets.Rows) 
{ 
    TextBox txtmobileNumber = (TextBox)HandSetRow.FindControl("txtmobilenumber"); 

    //I need the linq statement here if the mobile number doesnt exists i need to 
    //throw an error saying mobile number doesnt exist within the dictionary 
    //iv been looking at linq statements for the past hour and im confused :(
} 

任何一個可以幫助我在這一個快速的解決方案?

+2

這裏你不需要LINQ,但是除非你給出一些有關字典結構的信息,任何人都可以回答這個問題嗎? – Jon

+0

注意,如果你的方法的結果如下所示,如果你的真實代碼是這樣的,那麼只需聲明它並刪除'new'部分就可以實例化Dictionary。 – Wasp

回答

0

下一次,發佈您的代碼到目前爲止,然後我們可以指出任何錯誤。

我不清楚電話號碼是在鑰匙還是在價值中,但這樣的事情應該工作。

檢查的重點:

string phoneNumberToLookFor = "12345678"; 
bool phoneNumberExists = GTNMobilelist.Any(kvp => kvp.Key == phoneNumberToLookFor); 

或者,通過價值檢查:

string phoneNumberToLookFor = "12345678"; 
bool phoneNumberExists = GTNMobilelist.Any(kvp => kvp.Value == phoneNumberToLookFor); 
3

這裏有使用LINQ沒有任何意義。改爲使用ContainsKeyContainsValue

if (!GTNMobilelist.ContainsValue(txtmobileNumber.Text)) 
    ShowErrorMessage();