select distinct xelement based on attribute

  <field name="value" value="A" />
  <field name="value" value="B" />
  <field name="text" value="C" />
  <field name="text" value="D" />
  <field name="key" value="E" />

When I filter I need to get one element for "value", one element for "text" and one element for "key" (Yes, "key" has only one element and it should be returned)

I tried

 elements.Select(item=>item.Attribute("name")).Distinct();

This returns only the XAttribute but I need the entire XElement.

Jon Skeet
people
quotationmark

It sounds like you need DistinctBy from MoreLINQ:

var distinct = elements.DistinctBy(x => x.Attribute("name").Value);

people

See more on this question at Stackoverflow