I have a PM system that is more like the Google Mail style. I mean messages are grouped into conversations. If a user receives a message, it shows up in their inbox. Also, the this user sends a message to someone who in turn replies back, then this message also shows up in their inbox.
Some messages are being retrieved in both queries which end up being grouped into one List. I tried to remove duplicates by doing what Jon Skeet did in removing duplicates from a list C# but still, I keep getting duplicates. Here's my code:
UPDATED:
public class Message : IEquatable<Message>
{
public int Id { get; set; }
[MaxLength(150)]
public string Subject { get; set; }
[MaxLength(3000)]
public string Content { get; set; }
public DateTime DateSent { get; set; }
public DateTime? LastViewed { get; set; }
public bool IsRead { get; set; }
public bool DisplayInInbox { get; set; }
public virtual User SentBy { get; set; }
public virtual User ReceivedBy { get; set; }
public int? ParentId { get; set; }
public override bool Equals(object other)
{
return Equals(other as Message);
}
public bool Equals(Message other)
{
if (object.ReferenceEquals(other, null))
{
return false;
}
if (object.ReferenceEquals(other, this))
{
return true;
}
return Id == other.Id;
}
public override int GetHashCode()
{
return this.Id;
}
}
//inside MessagingService public IList GetThreads(User user) { //Get all messages that are not replies. var tmp = _repository.GetMany(c => c.DisplayInInbox.Equals(true) && c.ParentId.Equals(null)); var threads = (from c in tmp where GetReplies(user, c.Id).Count() > 0 select c).ToList(); var threadsByUser = user.ReceivedMessages.Where(m => m.DisplayInInbox.Equals(true) && m.ParentId.Equals(null)).ToList(); threads.AddRange(threadsByUser); threads.Distinct().ToList(); return threads; }
Am I doing something wrong here?