如何在C#中获取包含KeyValuePair的List值?

问题描述 投票:0回答:1

我想在List中找到包含KeyValuePair的元素,但不知道如何通过" "来获取。

不能将运算符==应用于类型为'System.Collections.Generic.List'的操作数。

"错误。当我把方法改为_adjacencyMatrix.FindIndex(from.GetNumer())时,就会得到"

参数int不可分配给参数类型'System .Predicate'。

" 然后当我把方法改为_adjacencyMatrix.IndexOf(MakePair(from,to))时,我得到了"

参数类型'System.Collections.Generic.KeyValuePair'不可分配给参数类型'System.Collections.Generic.List'。

"的错误。我不知道该怎么办?你们有什么办法吗?我是C#新手,虽然我在C++中工作过。我把代码附在下面。可能应该用数字代替numer,但我不想在我的代码中改变它。

using System.Windows.Documents;

namespace GPS
{
    public class Vertex
    {
        private int _numer;

        public Vertex(int numer)
        {
            this._numer = numer;
        }

        public void Deconstruct()
        {
        }

        public int GetNumer()
        {
            return this._numer;
        }
    }
}
using System.Collections.Generic;
using GPS;

namespace GPS
{
    public class Edge
    {
        private float _distance;
        private KeyValuePair<Vertex, Vertex> _fromTo;

        public Edge(float distance, KeyValuePair<Vertex, Vertex> fromTo)
        {
            this._distance = distance;
            this._fromTo = fromTo;
        }

        public KeyValuePair<Vertex, Vertex> GetFromTo()
        {
            return _fromTo;
        }


        public float GetDistance()
        {
            return _distance;
        }

        public void SetDistance(float distance)
        {
            this._distance = distance;
        }

        public void Deconstruct()
        {
        }
    }
}
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net.Configuration;
 using System.Windows.Documents;
 using GPS;

 namespace GPS
 {
     public class Graph
     {
         private int _vertexNumber;
         private List _egdesMatrix;

         private List<List<Vertex>> _adjacencyMatrix;

         public Graf(int vertexNumber)
         {
             this._vertexNumber = vertexNumber;
             this._egdesMatrix = new List();
             this._adjacencyMatrix = new List<List<Vertex>>();
         }

         public void Deconstruct()
         {
         }

         public List GetEgdesMatrix()
         {
             return _egdesMatrix;
         }


         public bool IsOutOfRange(Vertex from, Vertex to)
         {
             return from.GetNumer() < 0 || to.GetNumer() < 0 ||
                    from.GetNumer() >= _vertexNumber || to.GetNumer() >= _iloscWierzcholkow;
         }

         private KeyValuePair<Vertex, Vertex> MakePair(Vertex from, Vertex to)
         {
             return new KeyValuePair<Vertex, Vertex>(from, to);
         }

         public bool IsThereAnEdge(Vertex from, Vertex to)
         {
             if (IsOutOfRange(from, to) != false) return false;
             for (var i = 0; i < _vertexNumber; i++)
             {
                 if (_adjacencyMatrix[from.GetNumer()]==to) ;
                 {
                     return true;
                 }
             }

             return false;
         }

 //list.Find(item => item > 20);
         public void DodajKrawedz(Vertex from, Vertex to, float odleglosc)
         {
             if (IsOutOfRange(from, to) == false && IsThereAnEdge(from, to) == false)
             {
                 _egdesMatrix.Add(MakePair(from, to));
             }
         }
     }
 }
c# list keyvaluepair
1个回答
0
投票

这是因为 _adjacencyMatrix 属于 List<List<Vertex>>. 所以当访问 _adjacencyMatrix[from.GetNumer()] 你得到 List<Vertex> 而它无法与顶点相比。

更新#1

好吧,根据你的评论,如果你想搜索矩阵并找到两个顶点的KeyValuePair,你可以这样做。

var edgesMatrix = new List<KeyValuePair<Vertex, Vertex>>();

var from = new Vertex(1);
var to = new Vertex(2);

var pairsFromMatrix = edgesMatrix
    .Where(em => em.Key == from && em.Value == to);
© www.soinside.com 2019 - 2024. All rights reserved.