Excuse my pseudo code below. I'm pretty sure there is a magical way to write this in a single linq statement that will also dramatically improve the performance. Here I have a list of millions of records in AList. The id may not be unique. What I'm after is the original list removing all duplicates (based on the id), but always grabbing the record with the earliest date. mystring is almost always a different value when there is a duplicate id.
public class A
{
public string id { get; set; }
public string mystring { get; set; }
public DateTime mydate { get; set; }
}
List<A> aListNew = new List<A>();
foreach (var v in AList)
{
var first = AList.Where(d => d.id == v.id).OrderBy(d => d.mydate).First();
// If not already added, then we add
if (!aListNew.Where(t => t.id == first.id).Any())
aListNew.Add(first);
}