2015-05-21 30 views
5

Mam problem z usunięciem duplikatów na mojej liście.Usuwanie duplikatów odniesień z listy C#

mam tę listę

List<SaveMongo> toReturn 

z mojej klasy SaveMongo że wygląda tak

public class SaveMongo 
{ 
    public ObjectId _id { get; set; } 
    public DateTime date { get; set; } 
    public Guid ClientId { get; set; } 
    public List<TypeOfSave> ListType = new List<TypeOfSave>(); 
    public List<ObjectId> ListObjSave = new List<ObjectId>(); 
    public SaveMongo() 
    { } 
} 

Ilekroć chcesz dodać element do mojej listy używam następujący kod

public static fctName(BsonDocument doc) 
{ 
    toReturn.Add(AddingSaveMongo(doc.GetValue("_id"))); 
} 

public static SaveMongo AddingSaveMongo(BsonValue ObjValue) 
{ 
    foreach (SaveMongo doc in SpeCollection.FindAll()) 
    { 
     foreach (var id in doc.ListObjSave) 
     { 
      if (id == ObjValue) 
       return (doc); 
     } 
    } 
    return (null); 
} 

Jednak czasami uzyskuję zduplikowane odwołania. Próbowałem użyć tego , aby je usunąć. Bezskutecznie.

Próbowałem też zrobić to

if (!toReturn.Contains(AddingSaveMongo(doc.GetValue("_id")))) 
    toReturn.Add(AddingSaveMongo(doc.GetValue("_id"))); 

Wciąż bez powodzenia. Ale ilekroć drukować referencje w mojej liście mam te wynikają enter image description here

enter image description here

Co jestem brakujące tutaj tak, że mam jeszcze duplikaty odniesień w moim liście?

+0

Ewentualnie, jeśli” ponowne użycie .netfw> 3.5, możesz użyć HashSet. Wtedy nie będziesz musiał się martwić o duplikaty. – cup

+2

'HashSet ' nadal będzie musiał wiedzieć, jak porównać te obiekty. –

Odpowiedz

6

Korzystanie ugrupowania:

toReturn = (from e in toReturn 
      group e by e._id into g 
      select g.First()).ToList(); 

Można także grupa przez dwa (lub więcej) pól:

toReturn = (from e in toReturn 
      // group by ID and Date component 
      group e by new { e._id, e.date.Date } into g 
      select g.First()).ToList(); 
8

Obecnie Distinct pasuje do twoich obiektów za pomocą object.Equals, która wykonuje odniesienie równości. Jednym ze sposobów powiedzenia mu dopasowania obiektów w oparciu o inne kryteria jest wdrożenie IEquatable<SaveMongo>. Ten przykład porównuje obiektów na podstawie ich Id:

public class SaveMongo : IEquatable<SaveMongo> 
{ 
    public ObjectId _id { get; set; } 
    public DateTime date { get; set; } 
    public Guid ClientId { get; set; } 
    public List<TypeOfSave> ListType = new List<TypeOfSave>(); 
    public List<ObjectId> ListObjSave = new List<ObjectId>(); 

    public override bool Equals(object obj) 
    { 
     if (ReferenceEquals(null, obj)) return false; 
     if (ReferenceEquals(this, obj)) return true; 
     if (obj.GetType() != this.GetType()) return false; 
     return Equals((SaveMongo) obj); 
    } 

    public override int GetHashCode() 
    { 
     return _id.GetHashCode(); 
    } 

    public bool Equals(SaveMongo other) 
    { 
     return _id.Equals(other._id); 
    } 
} 
Powiązane problemy