Il tuo primo impulso fu quasi corretta. :) Come per David B, se non dite LINQ esattamente come si definisce l'uguaglianza e poi dargli un po 'di XElements, sarà confrontarli per riferimento. Fortunatamente, puoi dire di usare criteri diversi specificando un IEqualityComparer
(in pratica, un oggetto che ha un metodo Equals che restituisce true iff due XElements sono uguali secondo la tua definizione e false altrimenti e un metodo GetHashCode che accetta un XElement e restituisce un codice hash basato sui criteri di uguaglianza).
Ad esempio:
var elements = xDocument.Descendants(w + "sdt")
.Union(otherDocument.Descendants(w + "sdt", new XElementComparer())
.RestOfYourCode
...
qualche altra parte nel vostro progetto
public class XElementComparer : IEqualityComparer‹XElement› {
public bool Equals(XElement x, XElement y) {
return ‹X and Y are equal according to your standards›;
}
public int GetHashCode(XElement obj) {
return ‹hash code based on whatever parameters you used to determine
Equals. For example, if you determine equality based on the ID
attribute, return the hash code of the ID attribute.›;
}
}
Nota: non ho il quadro in casa, in modo che il codice esatto non è testato e il codice IEqualityComparer è da here (scorrere verso il basso al secondo post).
Questo è stato perfetto. Grazie! –