When I use the following code I get the same items multiple times.
XElement neededFiles = new XElement("needed",
from o in _9nFiles.Elements()
join t in addedToSitePull.Elements()
on o.Value equals
t.Value
where o.Value == t.Value
select new XElement("pic", o.Value));
I’d like to get only unique items. I saw a Stack Overflow post, How can I do SELECT UNIQUE with LINQ?, that used it, and I tried to implement it, but the change had no affect.
The code:
XElement neededFiles = new XElement("needed",
(from o in _9nFiles.Elements()
join t in addedToSitePull.Elements()
on o.Value equals
t.Value
where o.Value == t.Value
select new XElement("pic", o.Value)).Distinct() );
I imagine the reason this doesn’t work is because
XElement.Equalsuses a simple reference equality check rather than comparing theValueproperties of the two items. If you want to compare the values, you could change it to:You could also create your own
IEqualityComparer<T>for comparing twoXElements by their values. Note this assumes all values are non-null:Then you could replace the existing call to
DistinctwithDistinct(new XElementValueEqualityComparer()).