I have a list of objects. These objects are made up of a custom class that basically contains two string fields String1
and String2
.
What I need to know is if any of these strings are duplicated in that list. So I want to know if objectA.String1 == objectB.String1
, or ObjectA.String2 == ObjectB.String2
, or ObjectA.String1 == ObjectB.String
", or ObjectA.String2 == ObjectB.String1
.
Also, I want to mark each object that contains a duplicate string as having a duplicate string (with a bool HasDuplicate
on the object).
So when the duplication detection has run I want to simply foreach over the list like so:
foreach (var item in duplicationList)
if (item.HasDuplicate)
Console.WriteLine("Duplicate detected!");
This seemd like a nice problem to solve with LINQ, but I cannot for the life of me figure out a good query. So I ve solved it using good-old foreach, but I m still interested in a LINQ version.