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/