如何声明数组列表 [英] How to declare list of arrays

查看:59
本文介绍了如何声明数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个情况,我有比较数据需求,对于每个数据需求,应该有n个数据. n应该在运行时给出.

让我们考虑一下,我需要

数量少于10的产品代码

价格大于100的产品代码

生产日期少于6个月的产品代码.

以此类推....

所以我想将所有这些数据保存在某种结构中.在运行时,当我输入为5时,应该有5个  prodcode数量< 10,5个  prodcode的价格为> 100和5个带有manu.date<的产品6个月.

我有获取数据的算法,但是我怀疑是否还有更好的方法来保存这些数据,而不是为每个数据分别创建数组?因为所有这些仅具有产品代码

谢谢.

解决方案

我不确定这是否有帮助.  如果可能的话,请描述您正在尝试做的事情,而不是描述如何做,也许我们可以提供更多帮助.  您是否要对数据进行单元测试,是否正在使用mstest或nunit样式测试?

使用系统;
使用System.Collections.Generic;
使用Microsoft.VisualStudio.TestTools.UnitTesting;

命名空间UnitTestProject1
{
    [TestClass]
    公共类UnitTest1
    {
        [测试方法]
        公共无效TestMethod1()
        {
            列表<产品> productList =新列表< Product>()
            {
                新产品()
                {
                    产品代码="A123",
                    数量= 4,
                    价格= 105,
                    ManufacturedDate = DateTime.Today.AddDays(-20)
                },
                                新产品()
                {
                    产品代码="B123",
                    数量= 3,
                    价格= 205,
                    ManufacturedDate = DateTime.Today.AddDays(-30)
                }
            };
            
            foreach(productList中的var产品)
            {
                //做一点事
            }
        }
    }

    公共类产品
    {
        公共字符串ProductCode {get;放; }
        public int数量{get;放; }
        公共十进制价格{放; }
        公共DateTime ManufacturedDate {get;放; }
    }
}

作为记录,您可以创建一个数组列表.  例如:

私有列表< string []> listOfArrays = new List< string []>(5);


Hi All,

I have a scenario where i have diff data need and for each data need there should be n number of data. The n should be given during runtime.

Let us consider this, i need 

productcode whose quantity is less than 10

productcode whose price is greater than 100

productcode with manufatureddate less than 6 mnths.

and so on ....

so i want to hold all these data in some structure. During runtime when i input as 5 then there should be 5  prodcode quantity <10, 5  prodcode with price> 100 and 5 prodcode  with manu.date < 6 mnths.

i have the algorithm to get the data but my doubt is whether there is any better way to hold these data instead of creating array separately for each? as all these will have the productcode only

Thanks.

解决方案

I am not sure if this helps.  If not maybe describe what you are trying to do rather than how and maybe we can help more.  Are you trying to data-drive a unit test and if so are you using mstest or nunit style tests?

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            List<Product> productList = new List<Product>()
            {
                new Product()
                {
                    ProductCode = "A123",
                    Quantity = 4,
                    Price = 105,
                    ManufacturedDate = DateTime.Today.AddDays(-20)
                },
                                new Product()
                {
                    ProductCode = "B123",
                    Quantity = 3,
                    Price = 205,
                    ManufacturedDate = DateTime.Today.AddDays(-30)
                }
            };
            
            foreach (var product in productList)
            {
                //Do Something
            }
        }
    }

    public class Product
    {
        public string ProductCode { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
        public DateTime ManufacturedDate { get; set; }
    }
}

for the record, you can make a list of arrays.  for example:

private List<string[]> listOfArrays = new List<string[]>(5);


这篇关于如何声明数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆