我创建了一个用作词典中键的类。
public class FourUintsOneDecimalKeyInfo
{
public uint IdOne { get; set; }
public uint IdTwo { get; set; }
public uint IdThree { get; set; }
public uint IdFour { get; set; }
public decimal DefinitionOne { get; set; }
public class EqualityComparerAssetTableInfo : IEqualityComparer<FourUintsOneDecimalKeyInfo>
{
public bool Equals(FourUintsOneDecimalKeyInfo x, FourUintsOneDecimalKeyInfo y)
{
return x.IdOne == y.IdOne &&
x.IdTwo == y.IdTwo &&
x.IdThree == y.IdThree &&
x.IdFour == y.IdFour &&
x.DefinitionOne == y.DefinitionOne;
}
public int GetHashCode(FourUintsOneDecimalKeyInfo x)
{
return (x.IdOne.ToString() +
x.IdTwo.ToString() +
x.IdThree.ToString() +
x.IdFour.ToString() +
x.DefinitionOne.ToString()).GetHashCode();
}
}
}
我这样用:
ConcurrentDictionary<FourUintsOneDecimalKeyInfo,uint> _derivativeIds = new ConcurrentDictionary<FourUintsOneDecimalKeyInfo, uint>(new FourUintsOneDecimalKeyInfo.EqualityComparerAssetTableInfo());
但是当我尝试查找一个值时,基于如下的键:
uint theID;
bool gotId = theDict.TryGetValue(keyInfoInfo, out theID);
它总是返回false(即使我确定我想要找到的值实际存在。 当我离开公共十进制DefinitionOne {get;组;分离出类并相应地调整GetHashCode(就像这样)......
public class FourUintsKeyInfo
{
public uint IdOne { get; set; }
public uint IdTwo { get; set; }
public uint IdThree { get; set; }
public uint IdFour { get; set; }
public class EqualityComparerAssetTableInfo : IEqualityComparer<FourUintsKeyInfo>
{
public bool Equals(FourUintsKeyInfo x, FourUintsKeyInfo y)
{
return x.IdOne == y.IdOne &&
x.IdTwo == y.IdTwo &&
x.IdThree == y.IdThree &&
x.IdFour == y.IdFour;
}
public int GetHashCode(FourUintsKeyInfo x)
{
//todo: make a smarter / less cpu consuming algo
return (x.IdOne.ToString() + x.IdTwo.ToString() + x.IdThree.ToString() + x.IdFour.ToString()).GetHashCode();
}
}
......它确实有用。
有什么建议吗?
亲切的问候,
Matthijs
这确实有效:
public int GetHashCode(FourUintsOneDecimalKeyInfo x)
{
//todo: make a smarter / less cpu consuming algo
return (x.IdOne.ToString() +
x.IdTwo.ToString() +
x.IdThree.ToString() +
x.IdFour.ToString()).GetHashCode() +
x.DefinitionOne.GetHashCode();
}
但这可能不是最佳做法......
Jon的:
奇怪的是......现在它确实有效。即使使用旧的GetHasCode方法。using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ShowError
{ 公共部分类Form1:表格 {
ConcurrentDictionary<FourUintsOneDecimalKeyInfo,uint> _dictWithIds = new ConcurrentDictionary<FourUintsOneDecimalKeyInfo, uint>(new FourUintsOneDecimalKeyInfo.EqualityComparerFourUintsOneDecimalKeyInfo());
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FourUintsOneDecimalKeyInfo theKey = new FourUintsOneDecimalKeyInfo() { IdOne = 1, IdTwo = 1, IdThree = 1, IdFour = 1, DefinitionOne = 15.25M };
_dictWithIds.TryAdd(theKey, 1);
uint theID;
bool GotTheID = _dictWithIds.TryGetValue(theKey, out theID);
GotTheID = GotTheID; //returns true :-S
}
}
public class FourUintsOneDecimalKeyInfo
{
public uint IdOne { get; set; }
public uint IdTwo { get; set; }
public uint IdThree { get; set; }
public uint IdFour { get; set; }
public decimal DefinitionOne { get; set; }
public class EqualityComparerFourUintsOneDecimalKeyInfo : IEqualityComparer<FourUintsOneDecimalKeyInfo>
{
public bool Equals(FourUintsOneDecimalKeyInfo x, FourUintsOneDecimalKeyInfo y)
{
return x.IdOne == y.IdOne &&
x.IdTwo == y.IdTwo &&
x.IdThree == y.IdThree &&
x.IdFour == y.IdFour &&
x.DefinitionOne == y.DefinitionOne;
}
public int GetHashCode(FourUintsOneDecimalKeyInfo x)
{
//todo: make a smarter / less cpu consuming algo
return (x.IdOne.ToString() +
x.IdTwo.ToString() +
x.IdThree.ToString() +
x.IdFour.ToString() +
x.DefinitionOne.ToString()).GetHashCode();
}
}
}
}
另一个额外的编辑:如何填充。
当我从数据库信息(MySQL)填充字典时,它似乎出错了。我这样填写:if(reader != null)
{
while(reader.Read())
theDict.TryAdd(new FourUintsOneDecimalKeyInfo
{
IdOne = (uint)reader[0],
IdTwo = (uint)reader[1],
IdThree = (uint)reader[2],
IdFour = (uint)reader[3],
DefinitionOne = (decimal)reader[4]
}, (uint)reader[5]);
reader.Close();
}