C#:比较两个自定义类的ArrayList并找到重复的 [英] C#: Compare two ArrayList of custom class and find duplicates
问题描述
public class ProductDetails
{
public string id;
public string description;
公开浮动利率;
}
ArrayList products1 = new ArrayList();
ArrayList products2 = new ArrayList();
ArrayList duplicateProducts = new ArrayList();
现在我想要的是获取所有的产品(所有的ProductDetails类的字段)都有重复描述在 products1
和 products2
。
我可以对于/ while循环来说,以传统的方式运行两个,但是如果我将在两个数组中拥有超过10k个元素,那么这将非常慢。
所以可能有一些东西可以用LINQ完成。
如果你想使用linQ,你需要写你自己的EqualityComparer,覆盖两种方法等于和 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)
{
//检查对象是否是同一个对象。
if(Object.ReferenceEquals(x,y))返回true;
//检查产品的属性是否相等。
return x!= null&&& y!= null&&& x.id.Equals(y.id)&& x.description.Equals(y.description);
}
public int GetHashCode(ProductDetails obj)
{
//如果描述字段不为空,则获取哈希码。
int hashProductDesc = obj.description == null? 0:obj.description.GetHashCode();
//获取idfield的哈希码。
int hashProductId = obj.id.GetHashCode();
//计算产品的哈希码。
return hashProductDesc ^ hashProductId;
}
}
现在,假设你有这个对象:
ProductDetails [] items1 = {new ProductDetails {description =aa,id = 9,rating = 2.0f},
新的ProductDetails {description =b,id = 4,rating = 2.0f}};
ProductDetails [] items = {new ProductDetails {description =aa,id = 9,rating = 1.0f},
new ProductDetails {description =c,id = rating = 2.0f}};
IEnumerable< ProductDetails> duplicateates =
items1.Intersect(items2,new ProductComparer());
I have two 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 what I want is to get all the products (with all the fields of ProductDetails class) having duplicate description in both products1
and products2
.
I can run two for/while loops as traditional way, but that would be very slow specially if I will be having over 10k elements in both arrays.
So probably something can be done with LINQ.
If you want to use linQ, you need write your own EqualityComparer where you override both methods Equals and 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 the objects are the same object.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether the products' properties are equal.
return x != null && y != null && x.id.Equals(y.id) && x.description.Equals(y.description);
}
public int GetHashCode(ProductDetails obj)
{
//Get hash code for the description field if it is not null.
int hashProductDesc = obj.description == null ? 0 : obj.description.GetHashCode();
//Get hash code for the idfield.
int hashProductId = obj.id.GetHashCode();
//Calculate the hash code for the product.
return hashProductDesc ^ hashProductId ;
}
}
Now, supposing you have this 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());
这篇关于C#:比较两个自定义类的ArrayList并找到重复的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!