【问题标题】:How could I get the List Value which contains KeyValuePair in C#?如何在 C# 中获取包含 KeyValuePair 的列表值?
【发布时间】:2020-05-22 19:36:05
【问题描述】:

我想在 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++ 中工作过。我附上下面的代码。可能应该有数字而不是 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));
             }
         }
     }
 }

【问题讨论】:

  • 使用KeyValuePair&lt;Vertex, Vertex&gt; 存储fromto 值看起来不正确。您可以使用元组或自己的类
  • 好的,我已经改了 ;)

标签: c# list keyvaluepair


【解决方案1】:

这是因为_adjacencyMatrix 的类型为List&lt;List&lt;Vertex&gt;&gt;

因此,当访问_adjacencyMatrix[from.GetNumer()] 时,您会得到List&lt;Vertex&gt;,它不能与 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);

【讨论】:

  • 嗯,你说得对,我想在 _edgesMatrix 而不是 _adjacencyMatrix 中找到。我现在有:_egdesMatrix.IndexOf(MakePair(from, to)); if (_egdesMatrix.FindIndex(from.GetNumer())); {返回真;它仍然不起作用。我在两个具有相同代码的程序中收到错误消息,显示“Argument type'System.Collections.Generic.KeyValuePair is notassignable to parameter type T”或“Cannot resolve IndexOf”或“Cannot Resolve FindIndex”。
  • 好的。现在我知道如何比较 KeyValuePair 的元素,如何使用通用解决方案对其进行编码,我的意思是:var pairsFromMatrix = _egdesMatrix//!!! .Where(em => em.Key == from && em.Value == to); if (_edgesMatrix.FindIndex(MakePair(from, to))) ; ; {返回真; }
  • @Olga 如果您需要知道,如果pairsFromMatrix 包含某些内容,您可以致电Any。代码将是这样的:if (edgesMatrix.Any()) return true;
  • 好的。我需要知道两个特定节点(顶点)之间是否存在连接,这就是为什么我需要查找索引(如果它存在于 _edgesMatrix 中)或使用 lambda 表达式来找出相同的东西,这意味着 from 和 to 之间是否存在连接顶点。那可能吗?如果是这样,我该怎么做?
  • 对不起,我在上一条评论中犯了一个错误。应该是if (pairsFromMatrix.Any())...。这意味着edgesMatrix 中至少存在一对与fromto 具有相同项目的项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多