开发者

List Duplicates on specific Class

开发者 https://www.devze.com 2022-12-13 22:57 出处:网络
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 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 开发者_如何学Goadding 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


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; }
}


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


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 };
 }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号