C#

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 14 July 2016

HashSet removing duplicates from custom class


Code
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace commongStringQuestion  
 {  
   class Program  
   {  
     class A :IEquatable<A>  
     {  
       public string Id { get; set; }  
       public string Name { get; set; }  
       public bool Equals(A other) {  
         return Id.Equals(other.Id) && Name.Equals(other.Name);  
       }  
       public override int GetHashCode()  
       {  
         return Id.GetHashCode();  
       }  
     }  
     static void Main(string[] args)  
     {  
       HashSet<A> obj = new HashSet<A>();  
       A obj3 = new A();  
       obj.Add(new A { Name = "A" ,Id= "1"});  
       obj.Add(new A { Name = "AB", Id = "1" });  
       obj.Add(new A { Name = "Abx", Id = "1" });  
       foreach (var item in obj)  
       {  
         Console.WriteLine(item.Name);  
       }  
       Console.WriteLine("===========================");  
       Console.ReadLine();  
     }  
   }  
 }  

Output







Adding same records now
   
using System; 
using System.Collections.Generic;   
 using System.Linq;  
 namespace LinqDetails  
 {  
   class Program  
   {  
     class A : IEquatable<A>  
     {  
       public string Id { get; set; }  
       public string Name { get; set; }  
       public bool Equals(A other)  
       {  
         return Id.Equals(other.Id) && Name.Equals(other.Name);  
       }  
       public override int GetHashCode()  
       {  
         return Id.GetHashCode();  
       }  
     }  
     static void Main(string[] args)  
     {  
       HashSet<A> obj = new HashSet<A>();  
       A obj3 = new A();  
       obj.Add(new A { Name = "A", Id = "1" });  
       obj.Add(new A { Name = "A", Id = "1" });  
       obj.Add(new A { Name = "A", Id = "1" });  
       foreach (var item in obj.OrderByDescending(x => x.Name))  
       {  
         Console.WriteLine(item.Name);  
       }  
       Console.WriteLine("===========================");  
       Console.ReadLine();  
     }   
   }  
 }   













UnionWith


 using System;   
  using System.Collections.Generic;   
  using System.Linq;  
 namespace LinqDetails  
 {  
   class Program  
   {  
     class A : IEquatable<A>  
     {  
       public string Id { get; set; }  
       public string Name { get; set; }  
       public bool Equals(A other)  
       {  
         return Id.Equals(other.Id) && Name.Equals(other.Name);  
       }  
       public override int GetHashCode()  
       {  
         return Id.GetHashCode();  
       }  
     }  
     static void Main(string[] args)  
     {  
       HashSet<A> obj = new HashSet<A>();  
       A obj3 = new A();  
       obj.Add(new A { Name = "A", Id = "1" });  
       obj.Add(new A { Name = "B", Id = "1" });  
       obj.Add(new A { Name = "C", Id = "1" });  
       HashSet<A> obj4 = new HashSet<A>();  
       obj4.Add(new A { Name = "C", Id = "1" });  
       obj4.Add(new A { Name = "D", Id = "1" });  
       obj4.UnionWith(obj);  
       foreach (var item in obj4.OrderByDescending(x => x.Name))  
       {  
         Console.WriteLine(item.Name);  
       }  
       Console.WriteLine("===========================");  
       Console.ReadLine();  
     }   
   }  
 }   











IntersectWith


 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace LinqDetails  
 {  
   class Program  
   {  
     class A : IEquatable<A>  
     {  
       public string Id { get; set; }  
       public string Name { get; set; }  
       public bool Equals(A other)  
       {  
         return Id.Equals(other.Id) && Name.Equals(other.Name);  
       }  
       public override int GetHashCode()  
       {  
         return Id.GetHashCode();  
       }  
     }  
     static void Main(string[] args)  
     {  
       HashSet<A> obj = new HashSet<A>();  
       A obj3 = new A();  
       obj.Add(new A { Name = "A" ,Id= "1"});  
       obj.Add(new A { Name = "B", Id = "1" });  
       obj.Add(new A { Name = "C", Id = "1" });  
       HashSet<A> obj4 = new HashSet<A>();  
       obj4.Add(new A { Name = "C", Id = "1" });  
       obj4.Add(new A { Name = "D", Id = "1" });  
       obj4.IntersectWith(obj);  
       foreach (var item in obj4.OrderByDescending(x => x.Name))  
       {  
         Console.WriteLine(item.Name);  
       }  
       Console.WriteLine("===========================");      
       Console.ReadLine();  
     }  
   }  
 }  



No comments:

Post a Comment