List of List C# question or something else any pointers in coding this
I need some help in coding this in C#
I have a list of Baskets and their Global_Weights in overall Basket in Kilograms:
Basket1, Basket2, Ba开发者_如何学运维sket3, Basket4
0.1, 0.3, 0.9,0.6
Each of the Basket consist of the following thing:
Name,Level1, level2,Local_Weights
Name: String type
Level1:Double Type
Level2:Double Type
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
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.
You would use a class Basket
that contains a list of objects of the class BasketInfo
and create a list of that Basket
class:
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>();
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. Good luck!
You could use something of this sort.
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
精彩评论