我们可以在两个泛型列表之间建立关系吗?

我们可以在两个列表之间建立任何关系吗?我只想查询列表并从其他列表中找到相关结果。请提出一些建议

class GeneralClass
    {
        //public List<int> Student_RollNumber = new List<int>();
        //public List<string> Student_Name = new List<string>();
        private List<int> _student_rollnumber = new List<int>();
        private List<string> _student_name = new List<string>();
        public List<int> Student_RollNumber
        {
            get { return _student_rollnumber; }
            set { _student_rollnumber = value; }
        }
        public List<string> Student_Name
        {
            get { return _student_name; }
            set { _student_name = value; }
        }
    }
private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                obj.Student_RollNumber.Add(int.Parse(txtbx_rollnum.Text));
                obj.Student_Name.Add(txtbx_SName.Text);
                MessageBox.Show("Data saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message from form");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
               var result = obj.Student_Name.Contains(txtbx_find.Text).ToString();
              MessageBox.Show(result.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

在这里,我想要学生的名字,当我搜索Rollnumber.Can我们有一个列表中的多个列。


为什么不创建一个新的自定义类并将其用作通用参数?

class Student
{
  public Student(int rollNumber, string name)
  {
    Name = name;
    RollNumber = rollNumber;
  }

  public string Name { get; private set; }
  public int RollNumber { get; private set; }
}

class GeneralClass
{
  private List<Student> students = new List<Student>();
  public List<Student> Students
  {
    get { return students; }
  }
}

private void btn_save_Click(object sender, EventArgs e)
{
  try
  {
    int rollNumber = int.Parse(txtbx_rollnum.Text);
    string name = txtbx_SName.Text;
    obj.Add(new Student(rollNumber, name));
    MessageBox.Show("Data saved");
   }
   catch (Exception ex)
   {
     MessageBox.Show(ex.Message, "Message from form");
   }
}
链接地址: http://www.djcxy.com/p/21001.html

上一篇: Can we make a relation between two Generic Lists?

下一篇: Pasting textbox values to access database