简体   繁体   English

为数组c#中的每个项目创建类列表

[英]Create Class List for Each Item in Array c#

I have a static array of string[] items as below: 我有一个string []项目的静态数组,如下所示:

public string[] stringItems = {"sItem1", "sItem2", "sItem3"}

I also have a class list the following. 我也有以下课程清单。 Each array item will need to have this class list object: 每个数组项都需要具有此类列表对象:

public class PriceList
{
    public DateTime listDate { get; set; }
    public decimal listPrice { get; set; 
}

public override string ToString()
{
    return String.Format("Date: {0}; Price: {1};", listDate, listPrice);
}

I set the data using the following: 我使用以下方法设置数据:

 dataList.Add(new PriceList() { listDate = today, listPrice = price, theVolume = volume });

Can anyone help me figure out how to set the data for each index in the array using a for loop? 谁能帮我弄清楚如何使用for循环为数组中的每个索引设置数据? I think each array item will need to have it's own Prices list class, but I'm not sure how to set and call them. 我认为每个数组项目都需要拥有自己的价格列表类,但是我不确定如何设置和调用它们。 It might be easiest for me to set it up as a method with parameters and call that for each array item. 对于我来说,将其设置为带有参数的方法并为每个数组项调用它可能是最简单的。

Thanks. 谢谢。

To make my question more clear, I need the following: Table 为了使我的问题更清楚,我需要如下:

The properties of each sItem might have 100 or 100,000 list items. 每个sItem的属性可能具有100或100,000个列表项。 Each sItem will always have the same amount of list items. 每个sItem始终具有相同数量的列表项。

At different points in my program, I need to call the sItems directly for other data points. 在程序的不同点,我需要直接为其他数据点调用sItems。

public class Item {
   public string Name { get;set;}
   public List<PriceList> Prices {get;set;} = new List<PriceList>();
}

public string[] stringItems = {"sItem1", "sItem2", "sItem3"};
var items=stringItems.Select(x=>new Item {Name=x});

-- Adding --
items.First(i=>i.Name=="sItems1").Prices.Add(new PriceList() { ... });

You may be better to create a class that include the item description: 您最好创建一个包含项目说明的类:

public class PriceList
{
    public string itemDescription { get; set; }
    public DateTime listDate { get; set; }
    public decimal listPrice { get; set; }
    public int theVolume { get; set; }

    public override string ToString()
    {
        return String.Format("Item: {0}; Date: {1}; Price: {2}; Volume {3};", itemDescription, listDate, listPrice, theVolume);
    }
}

You could dynamically add entries to an ArrayList (in System.Collections ) or a List<T> ): 您可以将条目动态添加到ArrayList (在System.Collections )或List<T> ):

ArrayList items = new ArrayList();

or 要么

List<PriceList> items = new List<PriceList>();

Then you can loop through your static list 然后,您可以遍历静态列表

foreach (string s in stringItems)
{
    PriceList pl = new PriceList () { itemDescription = s, listDate = today, listPrice = price, theVolume = volume };
    items.Add (pl);
}

Both ArrayList and List you can reference directly at indexed positions with items[ n ] and you can also use foreach. ArrayListList都可以在带有item [ n ]的索引位置直接引用,也可以使用foreach。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM