我做了用作在字典中的关键一课。
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 {获取;集;}。部分出类,并相应调整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();
}
但它可能不是最好的做法?
乔恩:
奇怪的事情......现在它的工作。 即使与老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();
}