Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有一个好的方法来存储多个对象并且仍然易于访问_C#_Arrays_Class_Multidimensional Array - Fatal编程技术网

C# 有没有一个好的方法来存储多个对象并且仍然易于访问

C# 有没有一个好的方法来存储多个对象并且仍然易于访问,c#,arrays,class,multidimensional-array,C#,Arrays,Class,Multidimensional Array,也许这很难理解,但我想知道是否有一种方法可以像2d数组一样存储多个变量,但可以使用命令轻松访问它 例如: ItemID = "1";ItemName = "Apple";ItemPrice = 10; ItemLocation = "Storage"; ItemID = "2";ItemName = "Bread";ItemPrice = 20; ItemLocation = "Outs

也许这很难理解,但我想知道是否有一种方法可以像2d数组一样存储多个变量,但可以使用命令轻松访问它

例如:

ItemID = "1";ItemName = "Apple";ItemPrice = 10; ItemLocation = "Storage";
ItemID = "2";ItemName = "Bread";ItemPrice = 20; ItemLocation = "Outside";
ItemID = "3";ItemName = "TV";ItemPrice = 30; ItemLocation = "House";
因此,我可以使用类似Item.Name1的命令访问它;获取它和Item.IDApple的名称;要获取it的ID,名称和ID是唯一的。因此,稍后,当我获得项目的ID或名称时,我可以请求所有内容

大概是这样的:

int IID = Item.ID("Bread");
string IName = Item.Name(2);
int IPrice = Item.Price(IID);
string ILocation = Item.Location(IID);
//So .ID return the ID but requires string Name
//And everything else requires the ID
var itemList = new ItemList();
itemList.Add(new Item { ItemId = 1, ItemName = "Some Item", ItemLocation = "Wherever" });
itemList.Add(new Item { ItemId = 2, ItemName = "Some Other Item", ItemLocation = "" });

var item = itemList.GetByName("Some Item");
提前谢谢

-咖啡


Sry为我的糟糕语法仍在学习:x

您可以创建一个自定义列表:

using System.Collections.Generic;
using System.Linq;

...

public class Item
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public string ItemLocation { get; set; }
}

public class ItemList : List<Item>
{
    public Item GetByName(string name)
    {
        return this.FirstOrDefault(x => x.ItemName == name);
    }

    public Item GetById(int id)
    {
        return this.FirstOrDefault(x => x.ItemId== id);        
    }
}

您可以创建自定义列表:

using System.Collections.Generic;
using System.Linq;

...

public class Item
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public string ItemLocation { get; set; }
}

public class ItemList : List<Item>
{
    public Item GetByName(string name)
    {
        return this.FirstOrDefault(x => x.ItemName == name);
    }

    public Item GetById(int id)
    {
        return this.FirstOrDefault(x => x.ItemId== id);        
    }
}

我不确定我是否完全理解你的问题,但我在这里试探一下

给这只猫剥皮有很多方法,下面是一些想法:

您可以编写一个类、结构和元组来对项类型进行建模 您可以拥有一个使用ID作为键的字典,并将item对象存储为值。这将允许按ID轻松快速地查找,但按姓名或名称搜索的次数更少 将项目列表像列表一样存储在标准集合中,并使用linq查询筛选和查找项目或 编写您自己的集合类,该类可能从列表派生,然后您可以编写自定义方法,如ID、Name或 在像这样使用的集合类型上编写扩展方法ID、名称等
我不确定我是否完全理解你的问题,但我在这里试探一下

给这只猫剥皮有很多方法,下面是一些想法:

您可以编写一个类、结构和元组来对项类型进行建模 您可以拥有一个使用ID作为键的字典,并将item对象存储为值。这将允许按ID轻松快速地查找,但按姓名或名称搜索的次数更少 将项目列表像列表一样存储在标准集合中,并使用linq查询筛选和查找项目或 编写您自己的集合类,该类可能从列表派生,然后您可以编写自定义方法,如ID、Name或 在像这样使用的集合类型上编写扩展方法ID、名称等
据我所知,你是在一个数据行或元组对象之间!?是的,我想我在找一个元组。是否有方法重命名项目,使其不是Item1、Item2、Item3…..项目是否应该是项目列表的帮助器类?现在还不清楚项目是什么,基于名称,我假设它是列表中的一个特定项目。不是列表本身…Sry项只是我为命令选择的一个名称。例如,我希望它看起来像什么?我理解这一点,您位于Datarow或Tuple对象之间!?是的,我想我在找一个元组。是否有方法重命名项目,使其不是Item1、Item2、Item3…..项目是否应该是项目列表的帮助器类?现在还不清楚项目是什么,基于名称,我假设它是列表中的一个特定项目。不是列表本身…Sry项只是我为命令选择的名称,例如,我希望它是什么样子