.net c#从类对象列表中检索字段数据

Retrieving field data from a List of class objects
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我最终试图存储顶点数据:x、y、z.我正在比较两种不同的集合类型来存储数据:List<T>Dictionary<K,V>. 两者都包含相同的公共类数据.注意差异;我的列表使用一个构造函数(我稍后添加到列表中),我的 dict 使用一种方法来添加到 dict:

我的列表

class Node1: List<Node1>
{
    public int nid;
    public double x, y, z;
    public Node1() {}
    public Node1(string strLine)
    {
        nid = int.Parse(strLine.Substring(8, 8));
        x = double.Parse(strLine.Substring(24, 8));
        y = double.Parse(strLine.Substring(32, 8));
        z = double.Parse(strLine.Substring(40, 8));
    }
}

还有这个……字典

public struct loc
{
    public double x, y, z;
}
public class Node2: Dictionary<int,loc>
{
    public int nid;
    public void Add(string strLine)
    {
        // collect the dictionary KEY
        nid = int.Parse(strLine.Substring(8, 8));
        // collect the dictionary VALUE
        loc data;
        data.x = double.Parse(strLine.Substring(24, 8));
        data.y = double.Parse(strLine.Substring(32, 8));
        data.z = double.Parse(strLine.Substring(40, 8));
        this.Add(nid, data);
    }
}

在我的主要内容中,我读入了一个带有顶点数据的文本文件并执行以下操作(很多已被删除):

var Nodes1 = new Node1();
var Nodes2 = new Node2();

string strLine;

Nodes1.Add(new Node1(strLine));
Nodes2.Add(strLine);

两个集合现在都包含相同的数据.

现在问题来了.我正在寻找具有等于 1001 的 int"nid"的类的特定实例,并且我想找到相应双精度"x"的值.为了稍微澄清一下(因为我只是让自己感到困惑),我可以通过执行以下操作来检查是否存在包含 nid=1001 的实例:

// does the entry exist - LIST
bool here1 = Nodes1.Exists(var => var.nid == 1001);

// does the entry exist - DICTIONARY
bool here2 = Nodes2.ContainsKey(1001);

要从包含 nid=1001(对于 dict)的对象中获取双"x",我执行以下操作:

double here3 = Nodes2[1001].x;

但我不知道如何从 List 集合中获取数据.也许是因为 List 集合不是为此目的而设计的.但是,如果可能的话,我如何从 List 中得到同样的结果?

也许有人也可以解释另一种(更好的)方法来做到这一点?我仍然不太了解为什么/何时使用 LIST 和 DICTIONARY 集合,因为似乎有 100 种方法可以做同样的事情.

解决过程1
        int index = Nodes1.FindIndex(var => var.nid == 1001);
        double here4 = Nodes1[index].x;

I am ultimately trying to store Vertex data: x, y, z. I am comparing two different collection types to store the data: List<T> and Dictionary<K,V>. Both contain the same public class data. Note the differences; my list uses a constructor (I add to the list later) and my dict uses a method to ADD to the dict:

my LIST

class Node1: List<Node1>
{
    public int nid;
    public double x, y, z;
    public Node1() {}
    public Node1(string strLine)
    {
        nid = int.Parse(strLine.Substring(8, 8));
        x = double.Parse(strLine.Substring(24, 8));
        y = double.Parse(strLine.Substring(32, 8));
        z = double.Parse(strLine.Substring(40, 8));
    }
}

and there's this one... DICTIONARY

public struct loc
{
    public double x, y, z;
}
public class Node2: Dictionary<int,loc>
{
    public int nid;
    public void Add(string strLine)
    {
        // collect the dictionary KEY
        nid = int.Parse(strLine.Substring(8, 8));
        // collect the dictionary VALUE
        loc data;
        data.x = double.Parse(strLine.Substring(24, 8));
        data.y = double.Parse(strLine.Substring(32, 8));
        data.z = double.Parse(strLine.Substring(40, 8));
        this.Add(nid, data);
    }
}

In my main I read in a text file with the Vertex data and do the following (a lot has been removed):

var Nodes1 = new Node1();
var Nodes2 = new Node2();

string strLine;

Nodes1.Add(new Node1(strLine));
Nodes2.Add(strLine);

Both collections now contain the same data.

Now here comes the question. I am looking for a specific instance of the class with the int "nid" that equals 1001 and I want to find the value of the corresponding double "x". To clarify that somewhat (because I just confused myself), I can check to see if an instance that contains nid=1001 exists by doing the following:

// does the entry exist - LIST
bool here1 = Nodes1.Exists(var => var.nid == 1001);

// does the entry exist - DICTIONARY
bool here2 = Nodes2.ContainsKey(1001);

To get the double "x" from the object that contains nid=1001 (for the dict) I do:

double here3 = Nodes2[1001].x;

But I cannot figure out how to get the data from the List collection. Perhaps it is because the List collection was not intended for this purpose. But, if possible, how do I get the same out of the List?

Can someone also, perhaps, explain a another (better) way to do this? I still don't have a great understanding of WHY/WHEN to use the LIST and DICTIONARY collections as there seems to be 100 ways to do the same thing.

Solutions1
        int index = Nodes1.FindIndex(var => var.nid == 1001);
        double here4 = Nodes1[index].x;
转载于:https://stackoverflow.com/questions/15239442/retrieving-field-data-from-a-list-of-class-objects

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入