C# C字典图
我有一本字典:C# C字典图,c#,linq,dictionary,C#,Linq,Dictionary,我有一本字典: Dictionary<ICD_Map2, string> maps = new Dictionary<ICD_Map2, string>(); public class ICD_Map2 { public string call_type {get; set; } public string destination{get; set;} } maps.Add(new ICD_Map2()
Dictionary<ICD_Map2, string> maps = new Dictionary<ICD_Map2, string>();
public class ICD_Map2
{
public string call_type {get; set; }
public string destination{get; set;}
}
maps.Add(new ICD_Map2() {call_type = "Mobile SMS", destination = "Australia"},"Local Text");
maps.Add(new ICD_Map2() {call_type = "Mobile SMS", destination = "International"},"International Text");
所以我想要的是,当我传递两个变量时:
案例1 variable1=手机短信&&variable2=澳大利亚我想要一个返回本地文本的函数
案例2国际文本取决于我的输入变量匹配ICD_Map2定义移动短信和国际`
如果有多个结果,如何构造此映射函数以从一组结果返回第一个结果集?这是一个非常简单的示例,我有100多个映射。我认为这应该可行,将在一秒钟内再次检查:
maps.Where(a => (String.IsNullOrEmpty(var1) || String.Compare(a.Key.call_type, var1) == 0)
&& (String.IsNullOrEmpty(var2) || String.Compare(a.Key.destination, var2) == 0))
.FirstOrDefault().Value`
您必须在ICD_Map2上实现IEquitable接口并覆盖GetHashCode函数。最好也覆盖Equalsobject,尽管这不是必需的,因为字典使用通用IEquitable接口来查找密钥,尽管有很多方法可以实现这一点,我通常使用的最快最简单的方法是LINQ的FirstOrDefault,如下所示:
string var1 = "Mobile SMS";
string var2 = "Australia";
var item = maps.FirstOrDefault(e => e.Key.call_type == var1 && e.Key.destination == var2);
string result = (item == null) ? "No value" : item.Value;
在这种情况下,如果没有相应的匹配项,则结果将为null。要使用字典,键需要支持相等操作。例如:
public class ICD_Map2 : IEquatable<ICD_Map2>
{
public ICD_Map2(string callType, string destination) {
CallType = callType;
Destination = destination;
}
public override int GetHashCode() {
int result = 17;
result = -13 * result +
(CallType == null ? 0 : CallType.GetHashCode());
result = -13 * result +
(Destination == null ? 0 : Destination.GetHashCode());
return result;
}
public override bool Equals(object other) {
return Equals(other as ICD_Map2);
}
public bool Equals(ICD_Map2 other) {
if(other == null) return false;
if(other == this) return true;
return CallType == other.CallType && Destination == other.Destination;
}
public string CallType {get; private set; }
public string Destination{get; private set;}
}
var key = new ICD_Map2("Mobile SMS", "Australia");
string result;
if(maps.TryGetValue(key, out result)) {
Console.WriteLine("found: " + result);
}
反向查找有问题,除非您有第二个字典,否则无法进行优化。对性能的简单操作为:
string result = "International Text";
var key = (from pair in maps
where pair.Value == result
select pair.Key).FirstOrDefault();
if(key != null) {
Console.WriteLine("found: " + key);
}
总而言之:
static void Main()
{
Dictionary<ICD_Map2, string> maps = new Dictionary<ICD_Map2, string> {
{new ICD_Map2 ("Mobile SMS", "Australia"),"Local Text"},
{new ICD_Map2 ("Mobile SMS", "International"),"International Text"}
};
// try forwards lookup
var key = new ICD_Map2("Mobile SMS", "Australia");
string result;
if (maps.TryGetValue(key, out result))
{
Console.WriteLine("found: " + result);
}
// try reverse lookup (less efficient)
result = "International Text";
key = (from pair in maps
where pair.Value == result
select pair.Key).FirstOrDefault();
if (key != null)
{
Console.WriteLine("found: " + key);
}
}
生成自定义比较器:
public class CusComparer: IEqualityComparer<ICD_Map2>
{
public bool Equals(ICD_Map2 x, ICD_Map2 y)
{
return x.call_type.Equals(y.call_type)
&& x.destination.Equals(y.destination);
}
public int GetHashCode(ICD_Map2 obj)
{
return obj.call_type.GetHashCode()
^ obj.destination.GetHashCode();
}
}
因此,您可以获得:
var local = maps[new ICD_Map2() {
call_type = "Mobile SMS",
destination = "Australia"}];
正确使用字典时,结果不会超过1个-它是唯一的键到值映射。值不需要是唯一的。但是,使用字典确实毫无意义。。。因为这并没有从任何字典中受益code@MarcGravell出于好奇,他还可以用什么来模拟ICD_Map对象和字符串之间的映射?@MohammadBanisaeid他可以使键相等,他可以使用自定义比较器,他可以使用具有内置相等实现的元组,他可以使用匿名类型, etc@Mike-还有-为什么要使用字符串。在此处进行比较?显然,使用的方法是string.Equals…?@Mark-RE起诉字典是毫无意义的:是的,性能方面这是废话,但与备选方法相比,实现起来只需要一小部分时间:P RE string.Compare:他很可能希望平等地对待可变大小写字符串,也有文化信息可以考虑,也可能不适用于这种情况,一般来说,当比较字符串时,我发现使用字符串。明确地比较和指定所有可能的选项是最安全的方式。这不使用任何事实,它是一个字典,though@MarcGravell我完全同意,,我只是回答了这个问题最基本的部分。
var local = maps[new ICD_Map2() {
call_type = "Mobile SMS",
destination = "Australia"}];