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 Query
- 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?
- Get ICollection out from IQueryable<ICollection> LINQ Query
- 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
- How to get 0 out of a null linq query value when a condition isn't met and then set it to a ViewBag
More Query from same tag
- Passing data in ASP.NET MVC using LINQ - nuttiness
- How to do an aggregate query in linq
- C# LINQ left join 2 Datatables
- EF Core can't translate an expression to compare two collections which EF 6 could
- Linq to Entities: Mixed graph of Entities, EntityKeys, Attached, and New Objects
- Only initializers, entity members, and entity navigation properties are supported
- Pass linq anonymous object to function
- Which is faster: Union or Concat?
- C# - LINQ to unflatten items into a hierarchical collection
- linq join tables with multiple columns with or condition
- Group by Sum for Calculated field in Linq
- Combining Linq-Entity statements
- Select elements in an array where elements in another array are true
- C# Create Lambda Expression Dynamically
- Merge Complex Object List using Union / Intersect
- Cannot figure where clause in Linq that excludes values.
- LINQ to EF - Find records where string property of a child collection at least partially matches all records in a list of strings
- Search for similar values in 2 arrays
- Optimize Linq code
- How can I issue a LINQ query to a SQL Server to check if a column value starts with a word?
- How to use two variables in a foreach loop?
- Bind Linq Result to datagridview
- Populating a DropDownListFor with values from another model linked by a foreign key?
- This method is not supported against a materialized query result
- Using linq to filter List of List
- Make List<Student> Distinctable with IEquatable<Student>
- MVC3 Binding model (some fields) to the view
- Error in a LINQ query
- How to sort contours?
- OrderBy Enum but not by the value of my enum