Ключ, взятый из коллекции ключей, отсутствует в словаре
От: Воронин Иван Россия  
Дата: 30.03.22 10:11
Оценка: :)
Есть:

  Сортированный список:
private SortedList<TargetKey, Target> NewTarget = new SortedList<TargetKey, Target>();

  Структура значений:
    public struct Target
    {
        public String Code;
        public String Name;
        public String ItemCode;
        public String ItemName;
        public String DirecionCode;
        public String DirecionName;
        public String ParentCode;
        public Boolean isNP; //???
        public Byte Level;
        public DateTime DateBegin;
        public DateTime DateEnd;
        public void PrintToExcelSheet(ExcelWorksheet worksheet, int Row)
        {
            worksheet.Cells[Row, 1].Value = Row - 1;
            worksheet.Cells[Row, 2].Value = ItemCode;
            worksheet.Cells[Row, 3].Value = ItemName;
            worksheet.Cells[Row, 4].Value = DirecionCode;
            worksheet.Cells[Row, 5].Value = DirecionName;
            worksheet.Cells[Row, 6].Value = Level;
            worksheet.Cells[Row, 7].Value = DateEnd.ToString("dd.MM.yyyy");
            return;
        }
    }

  Класс ключей:
    public class TargetKey : IEquatable<TargetKey>, IComparable<TargetKey>
    {
        private String _Code;
        private String _Name;
        private byte _Level;

        public TargetKey()
        {
            this._Code = "";
            this._Name = "";
            this._Level = 0;
        }

        public TargetKey(String Code, String Name, byte Level)
        {
            this._Code = Code;
            this._Name = Name;
            this._Level = Level;
        }

        public void Set(String Code, String Name, byte Level)
        {
            this._Code = Code;
            this._Name = Name;
            this._Level = Level;
        }

        public String Code
        {
            get { return _Code; }
            set { _Code = value; }
        }

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public byte Level
        {
            get { return _Level; }
            set { _Level = value; }
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            TargetKey objAsTargetKey = obj as TargetKey;
            if (objAsTargetKey == null) return false;
            else return Equals(objAsTargetKey);
        }

        public bool Equals(TargetKey x)
        {
            return this._Code.Equals(x._Code) && this._Name.Equals(x._Name) && this._Level.Equals(x._Level);
        }

        public override int GetHashCode()
        {
            return this._Code.GetHashCode() + this._Name.GetHashCode() + this._Level.GetHashCode();
        }

        public int CompareTo(TargetKey compareTargetKey)
        {
            if (this == compareTargetKey) return 0;
            if (this < compareTargetKey) return -1;
            return 1;
        }

        public static bool operator ==(TargetKey a, TargetKey b)
        {
            if (a is null && b is null) return true;
            if (a is null || b is null) return false;
            if (a._Code == b._Code && a._Name == b._Name && a._Level == b._Level) return true;
            else return false;
        }

        public static bool operator !=(TargetKey a, TargetKey b) => !(a == b);

        public static bool operator <(TargetKey a, TargetKey b)
        {
            if (a is null || b is null) return false;
            if (a._Code.CompareTo(b._Code) < 0) return true;
            if (a._Name.CompareTo(b._Name) < 0) return true;
            if (a._Level.CompareTo(b._Level) < 0) return true;
            return false;
        }

        public static bool operator >(TargetKey a, TargetKey b)
        {
            if (a is null || b is null) return false;
            if (a._Code.CompareTo(b._Code) > 0) return true;
            if (a._Name.CompareTo(b._Name) > 0) return true;
            if (a._Level.CompareTo(b._Level) > 0) return true;
            else return false;
        }

        public static bool operator <=(TargetKey a, TargetKey b)
        {
            return (a < b) || (a == b);
        }
        public static bool operator >=(TargetKey a, TargetKey b)
        {
            return (a > b) || (a == b);
        }
    }


Список заполнен. Беру из него коллекцию ключей, прохожу по ней и смотрю значения, соответствующие ключу.

            ICollection<TargetKey> tKey = NewTarget.Keys;
            foreach (TargetKey tT in tKey)
            {
                MessageBox.Show(NewTarget[tT].Code + " | " + NewTarget[tT].Name + " | " + NewTarget[tT].Level);
            }


Вылетает исключение: System.Collections.Generic.KeyNotFoundException: "Данный ключ отсутствует в словаре."
Как в словаре может отсутствовать ключ, взятый из самого этого ключа?
Единственное, что могу предположить — некорректная перегрузка операторов сравнения и/или компаратора, но не понимаю, где именно.

Помогите найти проблемы.

ЗЫ. Если что, это не является моей профессиональной деятельностью.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.