I am trying to build a query that select unique groups of values. I know how to group values and select distinct values, but I don't see how to select distinct groups of values regarless of the order of the elements considered. More precisely:
My XML is something like this:
<A>
<B type="1">value1</prop>
<B type="2">value2</prop>
<B type="3">value3</prop>
<B type="4">value4</prop>
<C>
<D>XXX</D>
</C>
<C>
<D>YYY</D>
</C>
</A>
<A>
<B type="3">value3</prop>
<B type="4">value4</prop>
<B type="2">value2</prop>
<C>
<D>XXX</D>
</C>
<C>
<D>YYY</D>
</C>
</A>
Here I would like to make groups of <B> values from all <A> nodes and select only unique sets of values. For instance, here:
value1+value2+value3+value4
and
value2+value3+value4+value1
These two sets of values should be only unique if some or all of the values are different.
Thank you very much in advance for your help.
The following works:
var doc = XDocument.Parse(xml);
var comparer = new ElementsComparer();
var result = doc.Descendants("B")
.GroupBy(x => x.Parent)
.Distinct(comparer);
class ElementsComparer : IEqualityComparer<IGrouping<XElement, XElement>>,
IEqualityComparer<XElement>
{
public bool Equals(XElement lhs, XElement rhs)
{
return lhs.Value.Equals(rhs.Value);
}
public bool Equals(IGrouping<XElement, XElement> lhs,
IGrouping<XElement, XElement> rhs)
{
var x = lhs.OrderBy(a => a.Value);
var y = rhs.OrderBy(a => a.Value);
return x.SequenceEqual(y, this);
}
public int GetHashCode(XElement obj)
{
return obj.Value.GetHashCode();
}
public int GetHashCode(IGrouping<XElement, XElement> obj)
{
return 0;
}
}
This returns distinct groups. A group is seen as the same as another group if the elements in the group have the same values as the elements in the other group.
Unfortunatelly, we need a full blown IEqualityComparer implementation to achieve this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With