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 });
}
}