score:1

Accepted answer

you could just create an iequalitycomparer to use with the distinct(), that should get you what you need.

class program
{
    static void main(string[] args)
    {
        string xml = "<foo><property name=\"john\" value=\"doe\" id=\"1\"/><property name=\"paul\" value=\"lee\" id=\"1\"/><property name=\"ken\" value=\"flow\" id=\"1\"/><property name=\"jane\" value=\"horace\" id=\"1\"/><property name=\"paul\" value=\"lee\" id=\"1\"/></foo>";

        xelement x = xelement.parse(xml);
        var a = x.elements().distinct(new mycomparer()).tolist();
    }
}

class mycomparer : iequalitycomparer<xelement>
{
    public bool equals(xelement x, xelement y)
    {
        return x.attribute("name").value == y.attribute("name").value;
    }

    public int gethashcode(xelement obj)
    {
        return obj.attribute("name").value.gethashcode();
    }
}

score:1

your appoach is a bit weird, e.g., you don't need to project elements into new elements; it just works(tm) when you add existing elements to a new document.

i would simply group the <property> elements by the name attribute and then select the first element from each group:

var doc = xdocument.parse(@"<foo>...</foo>");

var result = new xdocument(new xelement("foo",
    from property in doc.root
    group property by (string)property.attribute("name") into g
    select g.first()));

score:1

i think you should remove the duplicates first, and then do your projection. for example:

var uniqueprops = from property in doc.root
                  group property by (string)property.attribute("name") into g
                  select g.first() into f
                  select new xelement("property", 
                      new xattribute("name", f.attribute("name").value),
                      f.attribute("value"));

or, if you prefer method syntax,

var uniqueprops = doc.root
    .groupby(property => (string)property.attribute("name"))
    .select(g => g.first())
    .select(f => new xelement("property", 
                     new xattribute("name", f.attribute("name").value),
                     f.attribute("value")));

Related Query

More Query from same tag