使用 IEqualityComparer来过滤PagedCollectionView里的重复数据
2010-10-22 19:53
330 查看
很多情况下我们获取的PagedCollectionView数据集内有很多重复的数据,我们可以使用 IEqualityComparer<T> 来过滤。
数据模型:
建一个过滤重复内容的类:
在Silverlight中调用:
数据模型:
public class Product { public string Name { get; set; } public int Code { get; set; } }
建一个过滤重复内容的类:
// Custom comparer for the Product class class ProductComparer : IEqualityComparer<Product> { // Products are equal if their names and product numbers are equal. public bool Equals(Product x, Product y) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(x, y)) return true; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; //Check whether the products' properties are equal. return x.Code == y.Code && x.Name == y.Name; } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. public int GetHashCode(Product product) { //Check whether the object is null if (Object.ReferenceEquals(product, null)) return 0; //Get hash code for the Name field if it is not null. int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode(); //Get hash code for the Code field. int hashProductCode = product.Code.GetHashCode(); //Calculate the hash code for the product. return hashProductName ^ hashProductCode; } }
在Silverlight中调用:
if (e.Result != null) { try { PagedCollectionView pcv = new PagedCollectionView(e.Result); List<Product> pp = new List<Product>(); foreach (Product p in pcv) { pp.Add(p); } this.listBox1.ItemsSource = pp.Distinct(new ProductComparer()); } catch { MessageBox.Show("获取XX内容失败!"); } }
相关文章推荐
- wpf,CollectionViewSource,使用数据过滤 筛选 功能。
- 用泛型的IEqualityComparer接口去重复项 .
- WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组
- datagrid 分组显示pagedCollectionView的使用、折叠组
- Silverlight中如何自己写方法将DataTable转换为PagedCollectionView数据(动态创建类)
- 温补而知新——之(Fragment+ViewPager使用,frgment切换listview数据重复加载问题png)
- 解决fragment与viewpager一起使用时fragment中的listview数据出现重复
- WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组
- 用泛型的IEqualityComparer<T>接口去重复项
- C#过滤重复数据,使用泛型
- 使用Oracle的row_number() over函数过滤重复数据,取最新的记录
- [转]WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组
- IEqualityComparer的使用
- [Silverlight]使用PagedCollectionView配合复选框实现动态筛选的解决方案
- 手动过滤重复字段数据(因为distinct关键字无法使用。。。)
- Android官方DataBinding(三):RecyclerView 使用ViewDataBinding更新数据
- 使用js,判断数组中是否有重复数据
- 使用ASP.NET Atlas ItemView控件显示集合中的单个数据
- ios开发-UIImageView的使用,点击图片刷新对应数据的过程
- PHP中使用Filter进行数据安全过滤