I am filtering a List (childList) based on the contents of another List (parentList).
The parentList can contain 0-n entities where items of the childList are referenced by an ID (btw. ID is in this case a string, you could also call it "Name" or whatever you like). I want to filter the childList in a way that the result is all elements that have at least one entry in the parentList.
The obvious solution is:
List<childItem> resultList = new List<childItem>();
foreach(var childItem in childList){
foreach(var parentItem in parentList){
if(childItem.ID.Equals(parentItem.ChildID,StringComparison.InvariantCultureIgnoreCase){
resultList.Add(childItem); //found entity
break; // search for next childItem
}
}
What I have done by now is:
// Group by childID and create List with all relevant IDs
var groupedParentList = parentList.AsParallel().GroupBy(p => p.ChildID).Select(group => group.First()).ToList();
// Filter childList on groupedParentList
var result = childList.AsParallel().Where(child => groupedParentList.AsParallel().Count(parent => parent.childID.Equals(child.ID, StringComparison.InvariantCultureIgnoreCase)) > 0).ToList();
But it's lasting rather long. The parentList contains about 75k entries and the childList about 4.5k entries. I just can't believe it actually takes time - imo this should be done in almost no time?
Maybe I am on the wrong track or am I missing some obvious performance-stopper?
Btw.: I have chosen the names parent- and childList just as an example. It's actaully no parent-child-relation.
Additional Question: Could it be faster to direcly check if the parentList contains the childList-Item? Something a lá:
var test = childList.RemoveWhere(child => !parentList.Contains(item => item.childID.Equals(child.ID));
So I would just skip the grouping? Any suggestions/ideas?