C#-将数据添加到列表中的列表 [英] C# - Adding data to list inside list
本文介绍了C#-将数据添加到列表中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将表格上的以下数据添加到名为Vehicles的列表中?
How can I add the following data on the table into a list called Vehicles?
public class criterias
{
public double values { get; set; }
public double time { get; set; }
}
public class movChannels
{
public string name { get; set; }
public IList<criterias> criteria = new List<criterias>();
}
public class stepsList
{
public string steps { get; set; }
public IList<movChannels> stepChannelsCriteria = new List<movChannels>();
}
public class vehicles
{
public int vehID { get; set; }
public string vehDescription { get; set; }
public IList<stepsList> vehValCriteria = new List<stepsList>();
}
现在,如何将显示在表格中的数据添加到名为Vehicles的列表中?稍后我将创建其他车辆...
Now, how can I add the data that I have in the table shown into a list called Vehicles? I will create other vehicles later...
推荐答案
您有几个错误的决定,一些是设计缺陷,一些是轻微的C#命名约定冲突.
You had several bad decisions, some were design flaws and some were minor C# naming convention violations.
值得一提的缺陷:
- vehID 应该是字符串,而不是int(例如"XPT")
- 移动具有名称,值和时间.它没有值和时间列表.
- vehID should have been a string and not int (Example "XPT")
- Movment has Name, Value and Time. It doesn't have a list of Values and Times.
创建:
List<Vehicle> vehicles = new List<Vehicle>();
Vehicle vehicle = new Vehicle()
{
Id = "XPT",
Description = "Average Car",
Steps = new List<Step>()
{
new Step() {
Name = "move car",
Movements = new List<Movement>()
{
new Movement("engage 1st gear", 1, 1),
new Movement("reach 10kph", 10, 5),
new Movement("maintain 10kph", 10, 12),
}
},
new Step() {
Name = "stop car",
Movements = new List<Movement>()
{
new Movement("reach 0kph", 10, 4),
new Movement("put in neutral", 0, 1),
new Movement("turn off vehicle", 0, 0),
}
}
}
};
vehicles.Add(vehicle);
实体:
public class Movement
{
public string Name { get; set; }
public double Values { get; private set; }
public double Time { get; private set; }
public Movement(string name, double values, double time)
{
Name = name;
Values = values;
Time = time;
}
}
public class Step
{
public string Name { get; set; }
public IList<Movement> Movements { get; set; }
}
public class Vehicle
{
public string Id { get; set; } // Should be changed to string
public string Description { get; set; }
public IList<Step> Steps { get; set; }
}
这篇关于C#-将数据添加到列表中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文