C# Linq unique not work on lists -
this question has answer here:
- distinct() doesn't work 4 answers
i trying following code check if list contains duplicated data:
internal class program { private static void main(string[] args) { var list = new list<obj>() { new obj() { id = "1", name = "1" }, new obj() { id = "1", name = "1" } }; console.writeline(allitemsareunique(list)); } public static bool allitemsareunique<t>(ienumerable<t> items) { return items.distinct().count() == items.count(); } } internal class obj { public string id; public string name; } and result true! why?
why?
by default, comparison use references , in case, 2 object references not same.
you need implement iequatable<t> provide type-specific equals() method distinct() use.
Comments
Post a Comment