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
- SelectListItem and linq any and select statement
- Using LINQ, how can I limit rows retrieved depending on a child table property?
- Get values from a column having comma separated values using Linq
- Using LINQ .Select and .Any in single line
- Linq Query Help Needed - Determining Exact Subsets
- LINQ Query Syntax to Lambda
- Convert a stored procedure than contains CASE values?
- List Manipulation in C# using Linq
- I need to create an interface in c# and a method to load a list of the interface
- Can I write case when query in entity framework core and linq?
- Where to process data? Db or locally?
- getting Key from collection of KeyValuePairs
- How to get row with bigger index using linq
- Entity Framework. Delete all rows in table
- Creating a LINQ to SQL compatible expression that accesses a property by its name
- Best way to handle redundant code that has repeated logic?
- Returning Element Values by Attribute Using C# and Linq
- linq search multiple columns
- Linq to list, then to DataGrid
- Multiple values for single key
- LINQ Query for Average of column with a join, grouping by the key of the joined table
- Translating Sql to Linq
- Get item index of a list within Select statement
- Search for multiple spaces in all list records
- How can i use in ( select method in linq?
- Linq query to get multiple columns in c# Entity Framework
- How to use predicates with LINQ to query CRM 2011
- Why use wrappers around the actual iterator functions in LINQ extension methods?
- LINQ read Select values
- C#--How do I use a Null Coalescing operator to allow use of an extension method that won't accept null parameters?