如何在C#中获取包含KeyValuePair的列表值? 更新#1

我想在List中找到keyvaluepair元素,并且想不通如何通过“

无法将运算符==应用于类型的操作数 'System.Collections.Generic.List'

”错误。当我将方法更改为_adjacencyMatrix.FindIndex(from.GetNumer())时,我得到“

参数int不可分配给参数类型'System 。谓词

“然后,当我将方法更改为_adjacencyMatrix.IndexOf(MakePair(from,to))时,我得到了”

参数类型'System.Collections.Generic.keyvaluepair'不是 可分配给参数类型'System.Collections.Generic.List

”错误。我不知道该怎么办。你们有什么想法吗?尽管我在C ++中工作,但我还是C#的新手。我附上以下代码。可能应该有数字而不是数字,但我不想在代码中更改它。

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));
             }
         }
     }
 }
iCMS 回答:如何在C#中获取包含KeyValuePair的列表值? 更新#1

这是因为_adjacencyMatrix的类型为List<List<Vertex>>

因此,访问_adjacencyMatrix[from.GetNumer()]时会得到List<Vertex>,无法与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);
本文链接:https://www.f2er.com/2296478.html

大家都在问