2012-10-11 129 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();