C# Linq unique not work on lists -


this question has answer here:

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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -