c# - 从 BindingList 中删除重复项

标签 c# distinct ienumerable enumerable bindinglist

BindingList有没有删除重复元素的解决方案?我试过:

 BindingList<Account> accounts = new BindingList<Account>();

 accounts.add(new Account("username", "password"));
 accounts.add(new Account("username", "password"));

 accounts = accounts.Distinct();

以上不起作用,因为 Distinct 返回 System.Collections.Generic.IEnumerable<T>而不是BindingList<T>

最佳答案

BindingList 有一个构造函数,它接受 IList<T> ,您可以转换 Enumerable<T> to a List

BindingList<Account> distinctAccounts = new BindingList<Account>(accounts.Distinct().ToList());

正如 King King 指出的那样,Distinct() 使用默认的相等比较器

The default equality comparer, Default, is used to compare values of the types that implement the IEquatable generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.

关于c# - 从 BindingList 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319359/

相关文章:

c# - 在 WinForms 的 ListView 中显示当前/选定的项目

mysql - 字符长度大于实际长度

c# - 我可以将 C# 函数标记为 "this function does not enumerate the IEnumerable parameter"吗?

c# - 将 StackPanel 子项绑定(bind)到 ObservableCollection

c# - System.Runtime.Cache 立即过期

mysql - 识别 2 个表中的不同行

c# - 将 IEnumerable<T> 转换/转换为 IEnumerable<U>?

.net - 如何汇总T的多个IEnumerables

c# - ASP.NET MVC 5 Identity with Model First

mysql - 在 MySQL 中结合使用 SELECT DISTINCT 和 UNION DISTINCT - 有什么效果吗?