[英]How to check if key exists in a dictionary when the key itself is a dictionary (C#)?
I have a dictionary [mapData]
as below and check key is a [Dictionary< string, DateTime >]
exists in [mapData]
, but it did not work: 我有一个如下的字典
[mapData]
,并且检查键是[mapData]
存在一个[Dictionary< string, DateTime >]
[mapData]
,但是它不起作用:
var mapData = new Dictionary<Dictionary<string, DateTime>, List<Student>>();
foreach (var st in listStudent)
{
// Create Key
var dicKey = new Dictionary<string, DateTime>();
dicKey.Add(st.Name, st.Birthday);
// Get mapData
if (!mapData.ContainsKey(dicKey)) // ===> Can not check key exists
{
mapData.Add(dicKey, new List<Student>());
}
mapData[dicKey].Add(st);
}
I tried with extension method as the below, but also not work: 我尝试使用扩展方法如下,但也无法正常工作:
public static bool Contains<Tkey>(this Dictionary<Tkey, List<Student>> dic, Tkey key)
{
if (dic.ContainsKey(key))
return true;
return false;
}
Any tips on these will be great help. 这些方面的任何提示都将为您提供很大的帮助。 Thanks in advance.
提前致谢。
You don't want to go there. 你不想去那里。 Complex objects are not meant to be keys in a dictionary.
复杂对象并不意味着是字典中的键。 I would suggest to move the dictionary to the value and create a more tree-like structure.
我建议将字典移至该值并创建一个更像树的结构。
There is one possibility to get this working, but I would advice against it for above reasons: the implementation of a custom IEqualityComparer
which compares the keys in the dictionary against another dictionary. 有一种方法可以使此工作正常进行,但是出于上述原因,我建议您这样做:实现自定义
IEqualityComparer
,该自定义IEqualityComparer
会将字典中的键与另一个字典进行比较。
this is because you try to find the key based on object reference not the key content. 这是因为您尝试根据对象引用而不是密钥内容来查找密钥。 Your dictionnary key is a composite of Key + value
(string + DateTime)
reference. 您的字典键是键+值
(string + DateTime)
引用的组合。
this wont work unless you rewrite and IEqualityComparer.
除非您重写和
IEqualityComparer.
否则IEqualityComparer.
无法正常工作IEqualityComparer.
var objectA = new ObjectA();
var objectB = new ObjectA();
objectA != objectB
unless you rewritte the equals. 除非重新写等号,否则
objectA != objectB
。
EDIT 编辑
This sample is comming from MSDN https://msdn.microsoft.com/en-us/library/ms132151(v=vs.110).aspx 此示例来自MSDN https://msdn.microsoft.com/zh-cn/library/ms132151(v=vs.110).aspx
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
BoxEqualityComparer boxEqC = new BoxEqualityComparer();
var boxes = new Dictionary<Box, string>(boxEqC);
var redBox = new Box(4, 3, 4);
AddBox(boxes, redBox, "red");
var blueBox = new Box(4, 3, 4);
AddBox(boxes, blueBox, "blue");
var greenBox = new Box(3, 4, 3);
AddBox(boxes, greenBox, "green");
Console.WriteLine();
Console.WriteLine("The dictionary contains {0} Box objects.",
boxes.Count);
}
private static void AddBox(Dictionary<Box, String> dict, Box box, String name)
{
try {
dict.Add(box, name);
}
catch (ArgumentException e) {
Console.WriteLine("Unable to add {0}: {1}", box, e.Message);
}
}
}
public class Box
{
public Box(int h, int l, int w)
{
this.Height = h;
this.Length = l;
this.Width = w;
}
public int Height { get; set; }
public int Length { get; set; }
public int Width { get; set; }
public override String ToString()
{
return String.Format("({0}, {1}, {2})", Height, Length, Width);
}
}
class BoxEqualityComparer : IEqualityComparer<Box>
{
public bool Equals(Box b1, Box b2)
{
if (b2 == null && b1 == null)
return true;
else if (b1 == null | b2 == null)
return false;
else if(b1.Height == b2.Height && b1.Length == b2.Length
&& b1.Width == b2.Width)
return true;
else
return false;
}
public int GetHashCode(Box bx)
{
int hCode = bx.Height ^ bx.Length ^ bx.Width;
return hCode.GetHashCode();
}
}
// The example displays the following output:
// Unable to add (4, 3, 4): An item with the same key has already been added.
//
// The dictionary contains 2 Box objects.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.