简体   繁体   English

清单C#问题的清单,或其他与此编码有关的指针

[英]List of List C# question or something else any pointers in coding this

I need some help in coding this in C# 我需要一些帮助在C#中进行编码

I have a list of Baskets and their Global_Weights in overall Basket in Kilograms: 我以公斤为单位列出了购物篮及其在整体购物篮中的Global_Weights

Basket1, Basket2, Basket3, Basket4 篮子1,篮子2,篮子3,篮子4

0.1, 0.3, 0.9,0.6 0.1、0.3、0.9、0.6

Each of the Basket consist of the following thing: 每个购物篮都包含以下内容:

Name,Level1, level2,Local_Weights 名称,级别1,级别2,Local_Weights

Name: String type 名称:字符串类型

Level1:Double Type 级别1:Double Type

Level2:Double Type 级别2:双字型

Local_Weights:Double Type 本地重量:双精度

Each Basket will consist of N number of (Name,Level1, level2,Local_Weights) and there are M Baskets N and M are variable which will be user Input 每个购物篮将由N个(名称,级别1,级别2,Local_Weights)组成,并且有M个购物篮N和M是变量,将由用户输入

Should I be using List of list for this or something else any pointer on how to go about coding this? 我应该为此使用列表列表还是其他有关如何对此进行编码的指针?

How will I be accessing the members inside each of the Basket and perform calculation on them. 我将如何访问每个购物篮中的成员并对其进行计算。

Something like this perhaps? 也许这样的事情?

class Basket
{
    private List<Payload> _payload = new List<Payload>();

    public List<Payload> Payload
    {
        get { return _payload; }
    }

    public double Weight
    { 
        get { /* calculate total weight */; }
    }
}

class Payload
{
    public string Name { get; set; }
    public double Level1 { get; set; }
    public double Level2 { get; set; }
    public double Local_Weight { get; set; }
}

And perhaps some usage, like this: 也许有些用法,像这样:

var baskets = new List<Basket>();

// Create a new basket with some payload
var newBasket = new Basket();
newBasket.Payload.Add(   
    new Payload {Name = "MyName", Level1 = 1, Level2 = 2, Local_Weight = 3});
baskets.Add(newBasket);

// Access all payloads in all baskets for example
foreach (var payload in baskets.SelectMany(basket => basket.Payload))
{
    Console.WriteLine(payload.Name);
}

Perhaps a book like Head First C# could also be of use. 也许像Head First C#这样的书也可能有用。 Good luck! 祝好运!

You would use a class Basket that contains a list of objects of the class BasketInfo and create a list of that Basket class: 你可以使用一个类Basket包含该类的对象列表BasketInfo并创建一个列表Basket类:

class Basket
{
    public Basket()
    {
        BasketInfos = new List<BasketInfo>();
    }

    public double GlobalWeight
    {
        get
        {
            return BasketInfos.Sum(x => x.LocalWeight); 
        }
    }

    public IList<BasketInfo> BasketInfos { get; set; }
}

class BasketInfo
{
    public string Name { get; set; }
    public double Level1 { get; set; }
    public double Level2 { get; set; }
    public double LocalWeight { get; set; }
}

var baskets = new List<Basket>();

您可以使用这种东西。

Dictionary<string, List<MyClass>> var = new Dictionary<string, List<MyClass>>();//where string key is the basket name, MyClass is a class containing properties like Name,Level1, level2,Local_Weights

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

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