I have this IEnumerable:
var collection = from obj in list
select new
{
Title = (string)obj.Element("title"),
otherAtt = (string)obj.Element("otherAtt"),
..
..
..
..
};
And I want to remove all the objects in ‘collection’ that has duplicate Title. And to leave the last that had a duplicates.
For example:
collection = {
{Title="aaa" ,otherAtt="1" ,....},
{Title="bbb" ,otherAtt="2" ,....},
{Title="aaa" ,otherAtt="3" ,....},
{Title="aaa" ,otherAtt="4" ,....},
{Title="ccc" ,otherAtt="5" ,....},
{Title="bbb" ,otherAtt="6" ,....}
}
And I need that the filter will cause the collection to look like that:
collection = {
{Title="aaa" ,otherAtt="4" ,....},
{Title="ccc" ,otherAtt="5" ,....},
{Title="bbb" ,otherAtt="6" ,....}
}
Thanks.
You can use the
Distinct()extension method, but you will have to write an IEqualityComparer so that Title is used for the comparison.I think this is pretty close to what you need: http://forums.asp.net/post/2276937.aspx