I saw the fastest way to compare two lists <> , but I find it difficult to adapt it to my situation. My problem is that lists have different types.
My lists are as follows:
List<Type1> firstList;
List<Type2> secondList;
Here is what I have now:
foreach (Type1 item in firstList)
{
if (!secondList.Any(x => x.Id == item.Id))
{
// this code is executed on each item in firstList but not in secondList
}
}
foreach (Type2 item in secondList)
{
if (!firstList.Any(x => x.Id == item.Id))
{
// this code is executed on each item in secondList but not in firstList
}
}
It works and that’s all, but O(n^2). Is there a way to make this more efficient? The solution to the above issues says to use .Except, but does not accept lambda.
Edit: I mentioned this above, but it is still marked as duplicate. I do not have two lists of the same object. I have two lists of different objects. Type1 and Type2 are different types. They just have an identifier that I need to map.