0

我做了一个类用作字典中的键。

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; set; } 部分并相应地调整 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();
        }
    }

...它确实有效。

有什么建议么?

亲切的问候,

马蒂斯

这确实有效:

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();
                    }
4

0 回答 0