1

If I try:

List<Student> students = new List<Student>();
List<Group> Groups = new List<Group>();
Groups = students.Remove(f => f.StudentID.Equals(studentID));

I get an error on this line: f => f.StudentID.Equals(studentID)

I am having difficulty from my previous posts here https://stackoverflow.com/questions/10116685/linq-deleted-users-still-associated-with-groups and here Delete method in WCF

So I thought maybe I could delete the students contained within groups but I get an error Cannot convert lambda expression to type Student because it is not a delegate type.

Just a quick up date as to what I am trying to do, lets say student A can belong to multiple Groups. Then if I delete that student the student should no longer list any groups. But it does. However if I search lets say Group Computing the student isnt there. Only seems to be when I take the student and search which groups he belongs to (even if he is deleted) it returns the group I originally added him to. THIS SHOULDNT HAPPEN ARGHH

Community
  • 1
  • 1
Kirsty White
  • 1,210
  • 3
  • 26
  • 54
  • Do you really need to delete the student or just trying to hide them when you display/return the results? – arb Apr 12 '12 at 17:29
  • I dont think you could hide them, if I GET a group from a student ID or student name it will return the group if I delete that student I can stil GET the group it is associated with. Dont think there is a way to hide that. – Kirsty White Apr 12 '12 at 17:31
  • Looks like the the answers already posted. To explain f is a boolean, it's the result of Equals. – Tony Hopkinson Apr 12 '12 at 17:33

3 Answers3

7

If you are trying to pass in the predicate, you have to use the RemoveAll method. Remove method takes in a single element

var numRemoved = students.RemoveAll(f => f.StudentID == studentID);

Also the RemoveAll method doesn't return the "updated" list, it updates the list. So students would be updated list i.e. it would not have students with StudentID == studentID.

If you want to preserve the students as is, copy the elements to a new list

var newStudentList = new List<Student>(students);

and then filter that list

P.S. The common objects in both list are same i.e. if an object is updated in one list, it would get updated in the second list too. If a second copy is needed, deep cloning has to be implemented. This is just two lists from same pool of objects.

var numRemoved = newStudentList.RemoveAll(f => f.StudentID == studentID);

With your updates, it seems you are looking for

students.RemoveAll(s => s.StudentID == studentID);
Groups.ForEach(g => g.Groupsz.RemoveAll(gs => gs.StudentID == studentID));
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • if I try that I get the error on the entire line saying cannot implicity convert type int to list – Kirsty White Apr 12 '12 at 17:32
  • See the update. Do you want to keep the student list as is i.e. you want to get a new copy of list without the students with StudentID == studentID? – amit_g Apr 12 '12 at 17:34
  • No, lets say **student A** can belong to multiple **Groups**. Then if I delete that student the student should no longer list any groups. But it does. However if I search **Group Computing** the student isnt there. Only seems to be when I take the student and search which groups he belongs to (even if he is deleted) it returns the group I originally added him to. THIS SHOULDNT HAPPEN ARGHH – Kirsty White Apr 12 '12 at 17:43
  • `var Groups = new List(students);` gives a best overload match error – Kirsty White Apr 12 '12 at 17:46
  • I am not sure if I understand the previous comment. For the overload, I did not notice that the second list is of type Group. It has to be of the same type. I have updated the answer. Please edit your question with what you are trying to do. I so far have been thinking that you are working on same list and filtering on it based on some criteria. – amit_g Apr 12 '12 at 17:50
  • That doesnt do anything amit. Doesnt delete the student doesnt take the student out of the group he was added to. – Kirsty White Apr 12 '12 at 18:00
  • 1
    That wasn't supposed to anything to Groups. With your code for Groups, my guess is that you want to update the students as well as Groups.Groupsz. I have updated the answer for that. – amit_g Apr 12 '12 at 18:30
2

Try with RemoveAll...........

L.B
  • 114,136
  • 19
  • 178
  • 224
2

List<T>.Remove() has a return type of bool. So you're not going to get a List<Group> from List<Student>.Remove().

You can use var removed = students.RemoveAll(f => f.StudentID.Equals(studentID)); to get a list of which students were removed from list.

But you'll need to do a little more to get that list of students to be a List<Group>. There is not enough information in your question to tell if a conversion possibility exists.

Khan
  • 17,904
  • 5
  • 47
  • 59