1

I'm looking for a LINQ delete method for this:

<ArrayOfGroup>
    <Group>
        <GroupName>c</GroupName>
        <StudentList>
             <Student>
                 <TimeAdded>0001-01-01T00:00:00</TimeAdded> // trying to delete from here
                 <StudentID>1</StudentID>
                 <FirstName>a</FirstName>
                 <LastName>b</LastName> // to here
                 <StudentGroup/>
             </Student>
        </StudentList>
    </Group>
</ArrayOfGroup>

For some reason when I delete it this way:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class Service : IService
{
    List<Student> students = new List<Student>();
    List<Group> Groups = new List<Group>();
    public void removeStudent(string studentID)
    {
        students.Remove(students.Find(f => f.StudentID.Equals(studentID)));
        //var result = students.Where(n => String.Equals(n.StudentID, studentID)).FirstOrDefault();
        //if (result == null)
        //{
        //    result.StudentGroup.Remove(StudentList.Student.Find(f => f.StudentID.Equals(studentID)));
        //}
        // students.RemoveAll(f => f.StudentID.Equals(studentID)); also tryed this
    }

It doesn't remove it from the Student List/ student area of my ArrayOfGroup, so I am fed up trying to solve why it isn't deleting and just trying to find a new way to delete it along with the original records. I feel like a criminal for doing this but if anyone can help would be much appreciated.

My Data Contracts looks like this:

[DataContract(Name="Student")]
public class Student
{
    public Student()
    {
        StudentGroup = new List<Group>();
    }

    [DataMember(Name = "StudentID")]
    public string StudentID { get; set; }

    [DataMember(Name = "FirstName")]
    public string FirstName { get; set; }

    [DataMember(Name = "LastName")]
    public string LastName { get; set; }

    [DataMember(Name = "TimeAdded")]
    public DateTime TimeAdded;

    public string TimeAddedString
    {
        get
        {
            return this.TimeAdded.ToString("dd/MM/yyyy hh:mm:ss");
        }
    }

    public List<Group> StudentGroup { get; set; }
}

[DataContract(Name = "Group")]
public class Group
{
    public Group() 
    {
        StudentList = new List<Student>();
    }

    [DataMember(Name = "GroupName")]
    public string GroupName { get; set; }

    public List<Student> StudentList { get; set; }
}

This is how a student is added to a group if it helps?

public void AddStudentToGroup(string group, string studentID, string firstName, string lastName)
{
    var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
    var result1 = students.Where(n => String.Equals(n.StudentID, studentID)).FirstOrDefault();
    if (result != null)
    {
        result.StudentList.Add(new Student() { StudentID = studentID, FirstName = firstName, LastName = lastName });
    }
    if (result1 != null)
    {
        result1.StudentGroup.Add(new Group() { GroupName = group });
    }
}  
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kirsty White
  • 1,210
  • 3
  • 26
  • 54
  • students.Remove(students.First(f => f.StudentID.Equals(studentID))); Try First instead of Find and see if it throws exception – Habib Apr 12 '12 at 15:28
  • 2
    http://stackoverflow.com/questions/853526/c-sharp-using-linq-to-remove-objects-within-a-listt This post solves a similar problem – JonVD Apr 12 '12 at 15:30
  • @Habib.OSU no exception is thrown, the student is deleted but for some reason remains in my ArrayOfGroup... – Kirsty White Apr 12 '12 at 15:56
  • @KirstyWhite, If I understand it correctly then you have ArrayOfGroup.Group.StudentList, you need to remove it like ArrayOfGroup.Group.StudentList.Remove(...), remove just from studentList wont remove it from the array, just tell me the whole structure of Array group, from XML it looks like ArrayOfGroup.Group.StudentList – Habib Apr 12 '12 at 16:17
  • I dont know how to do that tho. – Kirsty White Apr 12 '12 at 16:41

1 Answers1

1

You can use the Remove method on the List<T> object which takes a predicate like so:

students.RemoveAll(s => s.StudentId == 5);
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55