从第二种形式的列表中删除数据

我有一个表格,我在其中读取包含学生信息的 csv 文件,该文件有两列StudentNumberMark. 我想让用户单击第一个表单上的一个按钮,然后转到另一个名为的表单deleteRecord。在此表单上,用户将键入 aStudentNumber和 a Mark,并且与它们对应的记录将从列表中删除两条信息。

由于我是 C# 的新手,我不确定如何去做,所以我们将不胜感激。

我的列表:

public static List<string> studentInfo = new List<string>();

我将该列表中的所有数据存储在一个名为 lstMarks 的列表框中

我还想向用户确认记录已成功删除。


慕哥9229398
浏览 172回答 1
1回答

缥缈止盈

如果所有数据都存储在列表中,只需使用 LINQ 并为列表中的每个学生添加一个数字作为索引。首先,您需要创建一个类并将其(我推荐)放在一个文件夹中。 它看起来如何。然后你必须把属性放在类中:public class Student{&nbsp; &nbsp; public int StudentNumber {get; set;}&nbsp; &nbsp; public int Mark {get; set;}&nbsp; &nbsp; public int Index {get; set;}}现在用列表添加另一个类:partial class MainWindow : Window{&nbsp; &nbsp; private List<Student> _studentInfo = new List<Student>()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; new Student() {Index = 0, StudentNumber = 0, Mark = 0}&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }然后在 deleteRecord 代码的顶部添加 using 以及文件夹和两个类的名称:using ExampleFolder.Class;您需要调用 Student 类才能修改 StudentNumber 以及 Mark 和 Index。Student studentInfo = new Student();&nbsp; &nbsp; int iIndex = 0;&nbsp; &nbsp; var req = from info in studentInfo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where info.StudentNumber == txtStudentNumber && info.Mark == txtMarks&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select info.Index; // Starts with 0 for the first student in the list&nbsp; &nbsp; foreach(var num in req)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; iIndex = num;&nbsp; &nbsp; }&nbsp; &nbsp; studentInfo.Remove(studentInfo[iIndex]);&nbsp; &nbsp; MessageBox.Show("Deleted!");
打开App,查看更多内容
随时随地看视频慕课网APP