【发布时间】:2017-05-06 21:25:17
【问题描述】:
类似这样的:
string defectGuid = null;
var dictionary = new Dictionary<bool,string> {
{new ProjectDiscrepancyWrongLocation().Conditions(row), "88ff2dfb-6190-4ab6-b13b-68de1719eac2"},
{new DestructionLeakiness().Conditions(row), "af018ee7-7974-45f8-a508-18359cde4108"},
{new CoatingInsulationDefect().Conditions(row), "232b2b2e-abc0-46b2-8b8c-45fede83ad83"},
{new CathodicProtectionUnitUnderProtectionZone().Conditions(row), "78cb3548-0d61-4809-bf28-0590dfb52010"},
{new CoatingPaintDefect().Conditions(row), "83e82b20-efc9-4500-8ca5-bb29631ccc61"},
{new ProjectDiscrepancyEdgesOffset().Conditions(row), "cf9c76c3-79eb-4856-8dc6-c9c7321a3ed5"},
{new DocumentationDiscrepancyDiscrepanciesInDocumentation().Conditions(row), "d3c793bc-5840-413a-8cae-6962772b4a88"},
{new DestructionElementDestruction().Conditions(row), "6b8924f9-5bba-4db8-b33c-099ca17a58b9"},
{new IndicatorDefectDamage().Conditions(row), "8F2560F3-31A0-4489-9A77-387BE9347D38"}
};
foreach (var item in dictionary.Where(item => item.Key)) {
defectGuid = item.Value;
}
return defectGuid;
其中一个类如下:
public class DestructionLeakiness : IDefectTypeConditions {
public bool Conditions(string[] row) {
return Helpers.NormalizeLocalizedString(row[4]).Contains("aaaa");
}
}
问题是会有一个错误提示密钥已经存在。
更新:
我改变了以下方法:
private static string SetTypeOfDefect(string[] row)
{
string defectGuid = null;
var list = new List<Tuple<bool, string>>()
{
new Tuple<bool, string>(new ProjectDiscrepancyWrongLocation().Conditions(row), "88ff2dfb-6190-4ab6-b13b-68de1719eac2"),
new Tuple<bool, string>(new DestructionLeakiness().Conditions(row), "af018ee7-7974-45f8-a508-18359cde4108"),
new Tuple<bool, string>(new CoatingInsulationDefect().Conditions(row), "232b2b2e-abc0-46b2-8b8c-45fede83ad83"),
new Tuple<bool, string>(new CathodicProtectionUnitUnderProtectionZone().Conditions(row), "78cb3548-0d61-4809-bf28-0590dfb52010"),
new Tuple<bool, string>(new CoatingPaintDefect().Conditions(row), "83e82b20-efc9-4500-8ca5-bb29631ccc61"),
new Tuple<bool, string>(new ProjectDiscrepancyEdgesOffset().Conditions(row), "cf9c76c3-79eb-4856-8dc6-c9c7321a3ed5"),
new Tuple<bool, string>(new DocumentationDiscrepancyDiscrepanciesInDocumentation().Conditions(row), "d3c793bc-5840-413a-8cae-6962772b4a88"),
new Tuple<bool, string>(new DestructionElementDestruction().Conditions(row), "6b8924f9-5bba-4db8-b33c-099ca17a58b9"),
new Tuple<bool, string>(new IndicatorDefectDamage().Conditions(row), "8F2560F3-31A0-4489-9A77-387BE9347D38")
};
foreach (var item in list.Where(item => item.Item1))
{
defectGuid = item.Item2;
}
return defectGuid;
}
【问题讨论】:
-
当然,为什么不可能呢?你试过了吗?发生了什么?
-
是的,有两个键
-
当然可以,但字典中只能有两个实例
-
你认为字典是如何工作的,你想通过使用布尔值作为键类型的字典来解决什么问题?
-
字典键必须是唯一的。你是如何尝试使用它的?也许你误解了字典的用途......
标签: c# dictionary collections