You can try this code:
public static void Main(string[] args)
{
List<ObjectA> listA = new List<ObjectA>()
{
new ObjectA(){Item = "abc" },
new ObjectA(){Item = "ab" },
};
List<ObjectB> listB = new List<ObjectB>()
{
new ObjectB(){Item = "abc" },
};
// loop backwards removing entry if it is found in the other list
for (int i = listA.Count - 1; i >= 0; i--)
if (listB.Find(e => e.Item == listA[i].Item) != null)
listA.RemoveAt(i);
}
I run both (yours and mine) methods five times and got following results (everytime repeating algorithm in a loop, 100000 iterations) in milliseconds:
my method: 107 46 94 67 91
your method: 108 267 171 138 173
It also might be due to extra ToList()
call and creating new object newList
.
So, to sum up, if there is any improvement, it is very little and I wouldn't sacrifice readability provided by wonderful LINQ methods for that.
Also, internally they were designed to work as fast as possible, so I would rely on them :)