I have a list which contains duplicate item values (by ID), but with a different (or possibly equal) priority. Duplicate items with same or lower priority should be removed from the list.
For example:
var items = new {
new { Id=2, Priority=3 },
new { Id=4, Priority=4 },
new { Id=1, Priority=4 },
new { Id=2, Priority=5 },
new { Id=4, Priority=4 }
};
RemoveDuplicates(items);
// items should now contain distinct values,
// with highest possible priority
var items = new {
new { Id=1, Priority=4 }, // this one was unique
new { Id=2, Priority=5 }, // this one was duplicate with higher priority
new { Id=4, Priority=4 }, // this one was duplicate with same priority
};
Is it possible to do this using LINQ? I know I could sort the list by ID and then check adjacent items, but just wanted to check if this is possible.
(update: input values are not necessarily grouped by IDs)
1 Answer