In order to know how it works Distinct
in linq form exemple compare two instances, I don't understand why Distinct
needs GetHashCode
, since all it has to do is call Equals
method to compare.
public class book {
public int Id { get; set; }
public string name { get; set; }
public override bool Equals(object obj) {
var x = this;
var item = obj as book;
return this.Id == item.Id;
}
public override int GetHashCode() {
var x = this;
return this.Id.GetHashCode();
}
}
class Program {
static void Main(string[] args) {
book a = new book() { Id = 1 };
book b = new book() { Id = 2 };
book c = new book() { Id = 2 };
List<book> listOfBooks = new List<book>(){ new book(){Id=1},new book(){Id=1},c};
var asdsd = listOfBooks.Distinct().ToList();
bool x = b==c;
}
}