Reputation: 3472
I am trying to figure out why my delegate function does not work, any help would be appreciated, this is probably a small issue but I have been working on it for a while and connot figure it out, my code:
//remove all matching people from this list
public void RemovePeopleFromLookup(Predicate<PeopleDTO> _people)
{
//Lookup is an internal readonly ICollection of PeopleDTO
Lookup.RemoveAll(_people);
}
//call the method as below: //data is a collection of PeopleDTO
mylookupobj.RemovePeopleFromLookup(x => data.Any(y => y.Name == x.Name && x.Type == FieldElement.Strange));
For some reason all people get removed from this lookup, this is not correct, I only want to remove the people who are
EDIT:
The data collection can be an object of different types -> strange, noisy etc... The mylookupobj.Lookup data collection is similar to the data collection and contain contain multiple types hence why I wrote my query that way
EDIT2: I missed out this information which may be very important...
public class PersonDTO
{
//Name
//Type
//Age
//Desc
}
Inside the mylookupobj.Lookup - all the properties contain data, however inside the data collection only the Name + Type is present.
Upvotes: 2
Views: 1027
Reputation: 273784
A simpler and more efficient predicate would be:
x => (x.Type == FieldElement.Strange) && data.Any(y => y.Name == x.Name)
But I admit I don't see a principal problem with either.
Edit: seems 1 of the conditions has to be inverted.
x => (x.Type == FieldElement.Strange) && ! data.Any(y => y.Name == x.Name)
Upvotes: 3
Reputation: 13409
Please try this.
mylookupobj.RemovePeopleFromLookup(x => data.Contains(y => y.Name == x.Name)
&& x.Type == FieldElement.Strange);
Upvotes: 0
Reputation: 19712
The call to Any is the problem. Essentially it's running through the collection multiple times. i.e. if any of the objects in the collection matches the condition, remove the item. Try this:
mylookupobj.RemoveFieldFromLookup(y => y.Name == x.Name && x.Type == FieldElement.Strange);
Upvotes: 1