C#为工作人员提供多维列表



我希望设置一个员工列表以插入多个数据项目。例如,我想给员工一个ID,名称和技术技能列表以及个人技能列表。并非所有员工都具有相同数量的技术技能或个人技能,但能够拥有每个

的倍数

所以一个例子是:

employeeID, employeeName, techSkill1, techSkill2, persSkill1
employeeID, employeeName, techSkill1, persSkill1, persSkill2
employeeID, employeeName, techSkill1, techSkill2, techSkill3, persSkill1

这甚至可能吗?

使用类:

public class Employee
{
    /// <summary>
    /// employee's ID
    /// </summary>
    public int ID { get; set; }
    /// <summary>
    /// employuee's name
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// list of personal skills
    /// </summary>
    public List<string> PersSkills { get; private set; }
    /// <summary>
    /// list of tecnical skills
    /// </summary>
    public List<string> TechSkills { get; private set; }
    /// <summary>
    /// конструктор
    /// </summary>
    public Employee()
    {
        this.PersSkills = new List<string>();
        this.TechSkills = new List<string>();
    }
    /// <summary>
    /// конструктор
    /// </summary>
    public Employee(int id, string name, string[] persSkills, string[] techSkills)
    {
        this.ID = id;
        this.Name = name;
        this.PersSkills = new List<string>(persSkills);
        this.TechSkills = new List<string>(techSkills);
    }
}

用法:

List<Employee> employees = new List<Employee>();
employees.Add(new Employee(1, "Ivan", new string[] { "good friend" }, new string[] { "engineer" }));
employees.Add(new Employee(2, "Boris", new string[] { "personnel management", "tolerance" }, new string[] { "engineer", "programmer" }));

是的,这是可能的,您可以做这样的事情:

public List<Member> members = new List<Member>();
public Form1()
{
    InitializeComponent();
    Member me = new Member();
    me.ID = 3;
    me.Name = "Maarten";
    PersSkill skill1 = new PersSkill();
    skill1.Name = "Super Awsome Skill!";
    skill1.MoreInfo = "All the info you need";
    PersSkill skill2 = new PersSkill();
    skill1.Name = "name!";
    skill1.MoreInfo = "info";
    List<PersSkill> list = new List<PersSkill>();
    list.Add(skill1);
    list.Add(skill2);
    me.PersSkills = list;
}
public struct Member
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<TechSkill> PersSkills { get; set; }
    public List<TechSkill> TechSkills { get; set; }
}
public struct PersSkill
{
    public string Name { get; set; }
    public string MoreInfo { get; set; }
}
public struct TechSkill
{
    public string Name { get; set; }
    public string MoreInfo { get; set; }
}

P.S。使用 @General Phooomer的解决方案,这是一个更好的解决方案,但是我将答案留在这里,也许您可以对此做些事情/从中学习

最新更新