arrays - C#: Compare two ArrayList of custom class and find duplicates -


i have 2 arrays of arraylist.

public class productdetails {     public string id;     public string description;     public float rate; }  arraylist products1 = new arraylist(); arraylist products2 = new arraylist(); arraylist duplicateproducts = new arraylist(); 

now want products (with fields of productdetails class) having duplicate description in both products1 , products2.

i can run 2 for/while loops traditional way, slow specially if having on 10k elements in both arrays.

so can done linq.

if want use linq, need write own equalitycomparer override both methods equals , gethashcode()

 public class productdetails     {          public string id {get; set;}         public string description {get; set;}         public float rate {get; set;}     }  public class productcomparer : iequalitycomparer<productdetails> {      public bool equals(productdetails x, productdetails y)     {         //check whether objects same object.          if (object.referenceequals(x, y)) return true;          //check whether products' properties equal.          return x != null && y != null && x.id.equals(y.id) && x.description.equals(y.description);     }      public int gethashcode(productdetails obj)     {         //get hash code description field if not null.          int hashproductdesc = obj.description == null ? 0 : obj.description.gethashcode();          //get hash code idfield.          int hashproductid = obj.id.gethashcode();          //calculate hash code product.          return hashproductdesc ^ hashproductid ;     } } 

now, supposing have objects:

productdetails [] items1= { new productdetails { description= "aa", id= 9, rating=2.0f },                         new productdetails { description= "b", id= 4, rating=2.0f} };  productdetails [] items= { new productdetails { description= "aa", id= 9, rating=1.0f },                         new productdetails { description= "c", id= 12, rating=2.0f } };   ienumerable<productdetails> duplicates =     items1.intersect(items2, new productcomparer()); 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -