score:1
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")));
Source: stackoverflow.com
Related Articles
- Filtering out duplicate XElements based on an attribute value from a Linq query
- LINQ to remove duplicate rows from a datatable based on the value of a specific row
- filtering a linq query, based on the object created from each element in the linq query
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value
- LINQ Query to remove duplicate items in a List based on the value
- Linq query built in foreach loop always takes parameter value from last iteration
- Linq query to exclude from a List when a property value of List of different type are equal?
- How to query xsi:type from an attribute using Linq to XML?
- Dynamic LINQ query to get Field value from Database
- Check if results from LINQ query contains a value
- how to take 100 records from linq query based on a condition
- Get max attribute value from XML using LINQ
- linq remove items from query where any list value is present
- LINQ Sub-select from Dictionary based on value type
- using linq query to find value that differs from previously found value
- Linq query based on attribute
- Allow duplicate keys with ToDictionary() from LINQ query
- Linq query to find duplicate objects based on multiple fields AND property is null
- creating Linq to sqlite dbml from DbLinq source code
- Re-using LINQ query based on bool value
- Why does this LINQ query assign a value of 1 to a NULL value from the database?
- C# LINQ : Dynamically create property and values that includes sum of a value from a grouped query
- LINQ Query - Selecting a key based upon the value having a property that matches a string?
- How to add conditional condition to LINQ query based on a previously returned value
- Is there a way to add or remove a line of an LINQ to DB Query based on if the value being checked is null?
- How to write a generic LINQ query for filtering of data based on first name middle name and last name
- LINQ Query to return list based on attribute of DB field
- Extracting Week number from DateTime value in Linq to entity query
- LINQ query to return from one table based on matching another table
- Linq query to get value from nested object
- Get object from mongodb c#
- How to create SQL Database from Linq2Sql Model
- many to many Entity Framework GetUsersInRole()
- How to write Linq or Lambda expression for nested collections
- Grouping multiple records in one single record
- LINQ query cannot evaluate GET parameter in StartsWith
- Simple language identification using LINQ
- Object initializers in a LINQ query - is it possible to reuse calculated data?
- Select objects that contain specific child elements
- How to get all id in table1 and save to table2 using c# mvc?
- which in those queries is better in performance and why?
- LINQ or lambda expression with subquery and NOT IN logic
- Getting multiple attribute values under node
- LINQ order a collection of objects by date in descending order
- How to query an XDocument with LINQ when elements have a colon in their name?
- convert Dictionary<int, Enumerable> to Dictionary<int, Enumerable> inverting content
- Using a class in joining to database table with EF
- linq orderbyAscending?
- list populated from DB is making calls to DB again when queried
- How can I pass TotalRowCount to DataPager