如何检测 C# 中该字典键是否存在?
我正在使用 Exchange Web 服务托管 API 和联系人数据。我有以下代码,它有效,但并不理想:
foreach (Contact c in contactList)
{
string openItemUrl = "https://" + service.Url.Host + "/owa/" + c.WebClientReadFormQueryString;
row = table.NewRow();
row["FileAs"] = c.FileAs;
row["GivenName"] = c.GivenName;
row["Surname"] = c.Surname;
row["CompanyName"] = c.CompanyName;
row["Link"] = openItemUrl;
//home address
try { row["HomeStreet"] = c.PhysicalAddresses[PhysicalAddressKey.Home].Street.ToString(); }
catch (Exception e) { }
try { row["HomeCity"] = c.PhysicalAddresses[PhysicalAddressKey.Home].City.ToString(); }
catch (Exception e) { }
try { row["HomeState"] = c.PhysicalAddresses[PhysicalAddressKey.Home].State.ToString(); }
catch (Exception e) { }
try { row["HomeZip"] = c.PhysicalAddresses[PhysicalAddressKey.Home].PostalCode.ToString(); }
catch (Exception e) { }
try { row["HomeCountry"] = c.PhysicalAddresses[PhysicalAddressKey.Home].CountryOrRegion.ToString(); }
catch (Exception e) { }
//and so on for all kinds of other contact-related fields...
}
正如我所说,这段代码有效。现在,如果可能的话,我想让它变得不那么糟糕。
我找不到任何方法可以让我在尝试访问字典之前检查该键是否存在,以及如果我尝试读取它(使用 .ToString()
)并且它不存在则抛出异常:
500
字典中不存在给定的键。
我怎样才能重构这段代码以减少影响(同时仍然有效)?
I am working with the Exchange Web Services Managed API, with contact data. I have the following code, which is functional, but not ideal:
foreach (Contact c in contactList)
{
string openItemUrl = "https://" + service.Url.Host + "/owa/" + c.WebClientReadFormQueryString;
row = table.NewRow();
row["FileAs"] = c.FileAs;
row["GivenName"] = c.GivenName;
row["Surname"] = c.Surname;
row["CompanyName"] = c.CompanyName;
row["Link"] = openItemUrl;
//home address
try { row["HomeStreet"] = c.PhysicalAddresses[PhysicalAddressKey.Home].Street.ToString(); }
catch (Exception e) { }
try { row["HomeCity"] = c.PhysicalAddresses[PhysicalAddressKey.Home].City.ToString(); }
catch (Exception e) { }
try { row["HomeState"] = c.PhysicalAddresses[PhysicalAddressKey.Home].State.ToString(); }
catch (Exception e) { }
try { row["HomeZip"] = c.PhysicalAddresses[PhysicalAddressKey.Home].PostalCode.ToString(); }
catch (Exception e) { }
try { row["HomeCountry"] = c.PhysicalAddresses[PhysicalAddressKey.Home].CountryOrRegion.ToString(); }
catch (Exception e) { }
//and so on for all kinds of other contact-related fields...
}
As I said, this code works. Now I want to make it suck a little less, if possible.
I can't find any methods that allow me to check for the existence of the key in the dictionary before attempting to access it, and if I try to read it (with .ToString()
) and it doesn't exist then an exception is thrown:
500
The given key was not present in the dictionary.
How can I refactor this code to suck less (while still being functional)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
ContainsKey
:或
TryGetValue
:更新 :根据评论,这里的实际类不是
IDictionary
而是PhysicalAddressDictionary
,因此方法为包含
和TryGetValue
但它们的工作方式相同。用法示例:
更新 2: 这是工作代码(由提问者编译)
...内部条件根据需要对每个所需的键进行重复。每个 PhysicalAddressKey(家庭、工作等)仅执行一次 TryGetValue。
You can use
ContainsKey
:or
TryGetValue
:Update: according to a comment the actual class here is not an
IDictionary
but aPhysicalAddressDictionary
, so the methods areContains
andTryGetValue
but they work in the same way.Example usage:
Update 2: here is the working code (compiled by question asker)
...with the inner conditional repeated as necessary for each key required. The TryGetValue is only done once per PhysicalAddressKey (Home, Work, etc).
c.PhysicalAddresses
的类型是什么?如果是Dictionary
,那么您可以使用ContainsKey
方法。What is the type of
c.PhysicalAddresses
? If it'sDictionary<TKey,TValue>
, then you can use theContainsKey
method.我使用字典,由于重复性和可能丢失的键,我很快拼凑了一个小方法:
调用它:
完成工作。
I use a Dictionary and because of the repetetiveness and possible missing keys, I quickly patched together a small method:
Calling it:
Gets the job done.
改进的 JohanE 答案,具有泛型支持并作为扩展方法:
调用它:
An improved JohanE answer, with generics support and as an extension method:
Calling it:
PhysicalAddressDictionary.TryGetValue
PhysicalAddressDictionary.TryGetValue
这是我今天煮的一些东西。似乎对我有用。基本上,您重写基本命名空间中的 Add 方法来进行检查,然后调用基本命名空间的 Add 方法以实际添加它。希望这对你有用
Here is a little something I cooked up today. Seems to work for me. Basically you override the Add method in your base namespace to do a check and then call the base's Add method in order to actually add it. Hope this works for you