views:

43

answers:

4

Hi, I have a list List<T> instances

where T has a date variable and a string ID. Now I need the list to remove duplicates on the string ID and only keep the latest dates. Anyone know how?

I was thinking of creating a new list List<T> final and looping through the instances list. In the loop checking if the list contains an item with the ID and then adding the item or removing the duplicate item with a lower date.

However I don't know how to check for a contains on a variable of a class T. Do I have to do this with lambda expression? or overwrite the Equals() of List? Forgot how to do either actually. Any help?

Or a better idea is always welcom to ofcourse!

Thanks a lot lot lot

+3  A: 

Can you use .NET 3.5? This sounds like GroupBy on the string id, then Max on each grouping to get the latest date.

Tim Robinson
Can you give me an example? Because I'm not very familiar with it
WtFudgE
Please see my answer for an example. Cheers, Rohan.
rohancragg
A: 

Sounds like you should

0) create a map that will use the String ID as the key
1) loop thru the list, 
  2) check if there is something already in the map at the map location for ID 
  3) If there is nothing, add the item
  4) If there is something there, update the map with the most recent item, and discard the other item.

If this is coming out of a database, just let the database deal with it instead by doing what the other poster said.

Zak
Can't use the database itself, because this list is already querried out of another database. It's actually a lot of lists.
WtFudgE
+4  A: 

As suggested by Tim Robinson:

var instances = new List<Data>() {
 new Data() {
  Name = "Two",
  Date = new DateTime(1998, 1, 1)
 },
 new Data() {
  Name = "Two",
  Date = new DateTime(1997, 1, 1)
 },
 new Data() {
  Name = "One",
  Date = new DateTime(1998, 1, 1)
 },
 new Data() {
  Name = "One",
  Date = new DateTime(1997, 1, 1)
 },
 new Data() {
  Name = "Three",
  Date = new DateTime(1998, 1, 1)
 },
 new Data() {
  Name = "Three",
  Date = new DateTime(1997, 1, 1)
 }
};

var groupedMax = from i in instances
 group i by i.Name into g
 select new Data() {
  Name = g.Key, 
  Date = g.Max(i => i.Date)
 };


public class Data
{
    public string Name { get; set; }
    public DateTime Date { get; set; }
}
rohancragg
my hero! thanks for clearing that up
WtFudgE
+1  A: 

You can also try

public class MyClass
{
  public DateTime dateTime;
  public int ID;
}
private void button1_Click(object sender, EventArgs e)
{
  List<MyClass> list = new List<MyClass>();

  list.Add(new MyClass() { dateTime = new DateTime(2009, 01, 01), ID = 1 });
  list.Add(new MyClass() { dateTime = new DateTime(2009, 02, 01), ID = 1 });
  list.Add(new MyClass() { dateTime = new DateTime(2009, 02, 01), ID = 2 });

  var dd = from d in list
                     group d by d.ID into g
                     let MaxDate = g.Max(u => u.dateTime)
                     select new { g.Key, MaxDate };
 }
astander