我有一个包含一些成员的 Key 类。当我只有一个键对象成员时,我将如何过滤字典。这是一些代码:
class Key
{
public int a { get; set; }
public int b { get; set; }
}
public class KeyEqualityComparer : IEqualityComparer<Key>
{
public int GetHashCode(Key k)
{
return (k.a + k.b).GetHashCode();
}
public bool Equals(Key lhs, Key rhs)
{
return ((lhs.a == rhs.a) && (lhs.b == rhs.b));
}
}
static Dictionary<Key, int> Data = new Dictionary<Key, int>( new KeyEqualityComparer() );
static void Main(string[] args)
{
Data.Add(new Key() { a = 0, b = 0 }, 99);
Data.Add(new Key() { a = 1, b = 0 }, 99);
// select all keys value pairs where the key contains a == 0
}