How to delete an item from a generic list

后端 未结 9 460
死守一世寂寞
死守一世寂寞 2020-12-31 05:51

I have a generic list

How do I remove an item?

EX:

Class Student
{
    private number;
    public Number
    {
        get( return number;)
          


        
相关标签:
9条回答
  • 2020-12-31 06:26
    List<Student> students = new List<Student>();
    students.Add(new Student {StudentId = 1, StudentName = "Bob",});
    students.RemoveAt(0); //Removes the 1st element, will crash if there are no entries
    

    OR to remove a known Student.

    //Find a Single Student in the List.
    Student s = students.Where(s => s.StudentId == myStudentId).Single();
    //Remove that student from the list.
    students.Remove(s);
    
    0 讨论(0)
  • 2020-12-31 06:29

    Well, you didn't give your list a name, and some of your syntax is wonky.

    void main()
    {
       static List<Student> studentList = new List<Student>();
    }
    
    // later
    void deleteFromStudents(Student studentToDelete)
    {
       studentList.Remove(studentToDelete);
    }
    

    There are more remove functions detailed here: C# List Remove Methods

    0 讨论(0)
  • 2020-12-31 06:32

    Maybe you can use a Dictionary<string, Student> instead of the List<Student>.

    When you add a Student, add its ID or Name or whatever can uniquely identify it. Then you can simply call myStudents.Remove(myStudentId)

    0 讨论(0)
  • 2020-12-31 06:34

    To delete a row or record from generic list in grid view, just write this code-

    List<Address> people = new List<Address>();
    Address t = new Address();
    people.RemoveAt(e.RowIndex);
    grdShow.EditIndex = -1;
    grdShow.DataSource = people;
    grdShow.DataBind();
    
    0 讨论(0)
  • 2020-12-31 06:39


    From your comments I conclude that you read student name from input and you need to remove student with given name.

    class Student {
        public string Name { get; set; }
        public int Number { get; set; }
    
        public Student (string name, int number)
        {
            Name = name;
            Number = number;
        }
    }
    
    
    var students = new List<Student> {
        new Student ("Daniel", 10),
        new Student ("Mike", 20),
        new Student ("Ashley", 42)
    };
    
    var nameToRemove = Console.ReadLine ();
    students.RemoveAll (s => s.Name == nameToRemove); // remove by condition
    

    Note that this will remove all students with given name.

    If you need to remove the first student found by name, first use First method to find him, and then call Remove for the instance:

    var firstMatch = students.First (s => s.Name == nameToRemove);
    students.Remove (firstMatch);
    

    If you want to ensure there is only one student with given name before removing him, use Single in a similar fashion:

    var onlyMatch = students.Single (s => s.Name == nameToRemove);
    students.Remove (onlyMatch);
    

    Note that Single call fails if there is not exactly one item matching the predicate.

    0 讨论(0)
  • 2020-12-31 06:44

    Try linq:

      var _resultList = list.Where(a=>a.Name != nameToRemove).Select(a=>a);
    
    0 讨论(0)
提交回复
热议问题